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 TaskDoForLoopAsync ()
{
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 goodHave 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 matterWe 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 methodsHigh 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: