Home > Net >  Add the async and without distinction
Add the async and without distinction

Time:11-12

There are two methods
 
Public override Task GetClassA ()
{
The return of Task. The Run (()=& gt; {
ClassA a=new ClassA ();
Return a;
});
}


 
Public override async Task GetClassA ()
{
Return await Task. Run (()=& gt; {
ClassA a=new ClassA ();
Return a;
});
}


The two methods in addition to writing the other is the difference between different?

CodePudding user response:

Added async and await the return of the Task of the Result is a new good ClassA object because await such as Task. Run in the content of the execution of the
Instead of async and await the return of the Task of the Result is null because at the end of the function performs a Task, the contents have not yet begun to Run in the Run

CodePudding user response:

Asyn asynchronous, with the head don't understand, you can drop to the MSDN

CodePudding user response:

This will divide said,
Add do not add async, is the same,
but add the async, add do not add await, is not the same,

CodePudding user response:

Async just told that this may be an asynchronous,
When the async + await, is asynchronous,

CodePudding user response:

Writing is actually more appropriate field
 public override Task GetClassA () 
{
ClassA a=new ClassA ();
Return Task. FromResult (a);
}


Clear from convenient apt semantic perspective, async is clearly let certain Task< back;> The method is easier to read more apt a "syntactic sugar",

CodePudding user response:

Such as asynchronous method
 public static async Task t () 
{
Var web=new WebClient ();
Var r=await web. UploadDataTaskAsync (" http://localhost:12341 ", Encoding UTF8. GetBytes (" spsp1234 "));
Var s=Encoding. UTF8. Get string (r);
The Debug. Assert (s.S tartsWith (" path length="));
}


Here because there await to order statement syntax to deduce the concurrent asynchronous operation in reality, less obviously async/await a Task layer encapsulation, clearer,

There may be many await in a big way, even in the loop structure using await, and you give examples of "deliberately" are examples of don't have to await, actually this kind of method does not need to define the Task as a function return value, or can be written as
 public override Task GetClassA () 
{
Await the Tasks. The Yield ();
ClassA a=new ClassA ();
Return a;
}


If you don't have a problem in this manufacturing complex, then actually have trouble,

CodePudding user response:

Less an async above, should be changed to:
 public override async Task GetClassA () 
{
Await the Tasks. The Yield ();
ClassA a=new ClassA ();
Return a;
}


In short, you can define a
 public override ClassA GetClassA () 
the return type of the method, you are now defined as Task As a function return value should be increased in itself a layer of trival, of course, in order to achieve a certain interface specification and return to the Task, you can use await the Tasks. The Yield, actually can not consider Task. Run,

Again, there is no need to use Task. Run (... ) when, in fact use the async/await grammar more relaxed, apt,

CodePudding user response:

refer to the third floor is nu month god reply:
this said to separate,
Add do not add async, is the same,
but add the async, add do not add await, is not the same,

+ 1

As for your problem, two methods on the logic, but run on ,

The reason is:
A, await brings a context switch;
Two, await to capture the current run "context" (-- an optional ExecutionContext), so that later on again, the capture ensures the asynchronous code in operation and can have the same LogicalCallContext, SecurityContext etc., and return to the current SynchronizationContext support, etc., but capture also brought a bit of extra overhead,

So, don't need to worry about using async await, but, if is a method of the building Lord, you'd better not await,
Online roslyn request, hope that the compiler can automatically detect the tail await calls, and automatic optimization method for a writing, but the request is still in the state of "open" (https://github.com/dotnet/roslyn/issues/1981),


  •  Tags:  
  • C#
  • Related