1.

What are async-await?

Answer»

Async-await is used to perform action asynchronously. It is required when there are long running activities, processing them synchronously may take long time and become blocker to web access. In an asynchronous process, the application can continue with other work that doesn't depend on the web resource until the potentially blocking task finishes.

The async and await keywords in C# are the heart of async programming. By using those two keywords, you can use resources in the .NET Framework. Asynchronous methods that you define by using the async keyword are referred to as async methods.

The following EXAMPLE SHOWS an async method.

// Three things to note in the signature:   //  - The method has an async modifier.    //  - The return type is Task or Task<T>. (See "Return TYPES" section.)   //    Here, it is Task<int> because the return statement returns an integer.   //  - The method name ends in "Async."   async Task<int> AccessTheWebAsync()   {       // You need to add a reference to System.Net.Http to declare client.      HttpClient client = new HttpClient();      // GetStringAsync returns a Task<string>. That means that when you await the      // task you'll get a string (urlContents).      Task<string> getStringTask = client.GetStringAsync("https://msdn.microsoft.com");      // You can do work here that doesn't rely on the string from GetStringAsync.      DoIndependentWork();      // The await operator suspends AccessTheWebAsync.      // - AccessTheWebAsync can't continue until getStringTask is complete.      // - Meanwhile, control returns to the caller of AccessTheWebAsync.      // - Control resumes here when getStringTask is complete.       // - The await operator then retrieves the string result from getStringTask.      string urlContents = await getStringTask;      // The return statement specifies an integer result.      // Any methods that are awaiting AccessTheWebAsync retrieve the length value.      return urlContents.Length;   }

For a method to be async, it should have following characteristics:

  • The method signature includes an async modifier.
  • The return type is one of the following types:
    • if method have a return statement then Task<TResult>.
    • if no return statement  then Task.
  • The method usually includes at least one await expression.

The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization CONTEXT and uses time on the thread only when the method is active.

An async method typically returns a Task or a Task<TResult>. Inside an async method, an await operator is applied to a task that's returned from a call to another async method. You specify Task<TResult> as the return type if the method contains a return statement that specifies an operand of type TResult.

You use Task as the return type if the method has no return statement or has a return statement that doesn't return an operand. An async method can't declare in, ref or out parameters, but the method can call methods that have such parameters. Similarly, an async method can't return a value by reference, although it can call methods with ref return VALUES



Discussion

No Comment Found