Home > Net >  C # asynchronous method about the Task, async/await use doubts? Thank you consult
C # asynchronous method about the Task, async/await use doubts? Thank you consult

Time:11-06

In the learning Task and asynchronous programming knowledge, some confusion, such as I perform a time consuming method DoForLoop (), direct use of Task. The Run () can realize the asynchronous, async/await the new system, the need to write an asynchronous method DoForLoopAsync (), asynchronous method DoForLoopAsync still need Task. In the Run () to achieve specific functions and return Task So not reinvent the wheel? Not equal to the Task. The Run () performed directly by the? Didn't understand what mean...
Predecessor, for the correct use of the async/await, or async/await is just a specification, a kind of contract? Thank you, in addition to stick a test code, consult the three way difference, thank you
 
//way 1: direct Task. Run
Private void btnTest_Click (object sender, EventArgs e)
{
Task. Run (new Func (DoForLoop));
}

//way 2:
Private void btnTest2_Click (object sender, EventArgs e)
{
DoForLoopAsync ();
}

//way 3: add async and await execution
Private async void btnTest3_Click (object sender, EventArgs e)
{
Await DoForLoopAsync ();
}

//ordinary methods
Private string DoForLoop ()
{
For (int I=0; I & lt; 10000; I++)
{
Console. WriteLine (I);
}
Return "finished";
}

//asynchronous method
Private Task DoForLoopAsync ()
{
The return of Task. The Run (()=& gt;
{
For (int I=0; I & lt; 10000; I++)
{
Console. WriteLine (I);
}
Return "finished";
});
}

CodePudding user response:

Too entangled with not good

Have no what, what kind of seal, no seal, superfluous action, have returned to bad word "programmers write code, according to the logic is not let programmers so you write code, logic make you so write"

Task. The Run () itself can await

As to why don't you await, your logic is to tell you, I want to wait for the result, don't wait for the results don't await, want to wait for the result is await

The testing process, for example, all call you followed by a

Messagebox. Show (" finished ")
You have to see the effect, have not await, immediately bounced, there is await until you run ended in bounced

CodePudding user response:

A gleam of programmers never with nouns, the ans just tangle with logic, what asynchronous, synchronous this vs that, it doesn't matter

We only know that I have to do now is the logic of "I need to perform the function of a time-consuming longer, I hope he executed to play I can get the result of the execution, and I hope the program does not stuck there, while he perform the function of this long, I do other things (such as you accumulate 10000, at the same time I also move the mouse, the window can move)"

CodePudding user response:

Many people say it's syntactic sugar, it's because logically is such a logic, now it is good to write a await, and finish this time, that call a strange various ingenious methods
High ability of thread synchronization, thread semaphore
Ability to make correction in
Low ability for 4 timer, get a timer to brush, look at the results, not

So, syntactic sugar now, logic is the logic, but Microsoft says don't tangle, since logic is asynchronous wait, I will give you an asynchronous + waiting for two special token, simple and straightforward to do, don't split the towers

CodePudding user response:

In my own words to explain: before the function declaration async, explain the function may have asynchronous execution of a code block, when await statements have been made inside the function, "waiting for" await calls to get the result of this line, more important is to await this statement at the bottom of the line is equivalent to the asynchronous call the callback function of execution, but is written in the order form, if not await statement this function just like normal function, it must await in the statement of async function of use,
If you only perform an asynchronous Task, also don't care about his results, such as order after sending a text message, no follow-up action, doesn't matter, Task, Run or don't write await calls, can be,
If you want to do a get access token request immediately after the user information such as order operations, and interfaces are asynchronous, in the past with the callback function, let you call twice is not a place, may affect the readability, use the async/await can make them look like order,

CodePudding user response:

Difference is whether you await ah,,,,

CodePudding user response:

reference 5 floor is nu month god reply:
difference is whether you await ah,,,,



You mean the await is executed asynchronously?

CodePudding user response:

Led upstairs:
You mean the await is executed asynchronously?


Don't tangle, and blog garden powder is different, we don't tangle mistake, I only said that 1000 species, 1 w type of mistake, it is good to know that he is wrong, it is not necessary to move over such online, the right is effective than with the wrong

Move is on TV, oil and salt, soy sauce, MSG, chicken essence, hot pepper is not good, Chinese prickly ash is bad, can you tell me you eat all? White boiled?
So not tangle is good, is a correct habit, you normally do is normal, healthy,

And do you really think those with wrong every day, eat healthy? Wrong, I know a man, I never let him cook station edge drop, because he is garden powder and blog, something great, he is standing beside you you can't put salt, soy sauce, monosodium glutamate drops? So, do you think he's so healthy, wrong, he did not put MSG, he put the old dopted mother, he didn't put the soy sauce, he put a lobster sauce, (is dripping, old dopted mother didn't tell him on TV, tempeh than soy sauce, monosodium glutamate and terrible?)

CodePudding user response:

Async/await: written in the form of synchronous asynchronous operations

CodePudding user response:

refer to 6th floor ∽ twisted dough-strips a reply:
Quote: refer to fifth floor is nu month god reply:
difference is whether you await ah,,,,



You mean the await is executed asynchronously?

For your question, it is in this way,

I put the code of the building Lord, perfect, increased thread. Sleep
Then, you can see operation,

First of all, the interface is the (the red box)


And then I modify the code, that is, in DoForLoopAsync () method, add the thread. Sleep (2000);
 public _click () 
{
InitializeComponent();
}

Private void Form1_Load (object sender, EventArgs e)
{
BtnTest. Click +=btnTest_Click;
BtnTest2. Click +=btnTest2_Click;
BtnTest3. Click +=btnTest3_Click;
}

//way 1: direct Task. Run
Private void btnTest_Click (object sender, EventArgs e)
{
Var q=Task. Run (new Func (DoForLoop));
Test1. Text=q.R esult.
}

//way 2:
Private void btnTest2_Click (object sender, EventArgs e)
{
Var q=DoForLoopAsync ();
Test2. Text=q.R esult.
}

//way 3: add async and await execution
Private async void btnTest3_Click (object sender, EventArgs e)
{
Var q=await DoForLoopAsync ();
Test3. Text=q;
}

//ordinary methods
Private string DoForLoop ()
{
For (int I=0; I & lt; 10000; I++)
{
Console. WriteLine (I);

}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related