C#console application asynchronous programming: understand
modifier async
In the asynchronous programming, the
method cannot be defined as async
, which brings a challenge to the developers who want the program asynchronous. Main
async
This article discusses this issue in depth and discusses the replacement method of achieving asynchronous behavior in the console application.
Dormitory
async
In Visual Studio 2012, the method is prohibited as
or Main
, the language now supports the async
Task
method. This allows developers to write the following code: Task<T>
async
Main
This method seems to block the main thread, similar to the use of
<code class="language-csharp">class Program { static async Task Main(string[] args) { Bootstrapper bs = new Bootstrapper(); var list = await bs.GetList(); } }</code>
The alternative method of the asynchronous console application GetAwaiter().GetResult()
When the method is not feasible, the developer can adopt one of the following alternative methods:
Customized context: Create a customized "main loop" compatible with async
compatible for the console application. This cycle acts as a container for asynchronous methods. Main
async
By using instead of <code class="language-csharp">class Program { static void Main(string[] args) { MainAsync(args).GetAwaiter().GetResult(); } static async Task MainAsync(string[] args) { Bootstrapper bs = new Bootstrapper(); var list = await bs.GetList(); } }</code>
GetAwaiter().GetResult()
method is not suitable for specific implementation. Can use alternative methods. Wait()
The above is the detailed content of How Can I Implement Asynchronous Programming in C# Console Applications?. For more information, please follow other related articles on the PHP Chinese website!