篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了An entry point cannot be marked with the 'async' modifier相关的知识,希望对你有一定的参考价值。
I copied below code from this link.But when I am compiling this code I am getting an entry point cannot be marked with the ‘async‘ modifier. How can I make this code compilable?
class Program
{
staticasyncvoid Main(string[] args)
{
Task<string> getWebPageTask = GetWebPageAsync("http://msdn.microsoft.com");
Debug.WriteLine("In startButton_Click before await");
string webText = await getWebPageTask;
Debug.WriteLine("Characters received: " + webText.Length.ToString());
}
privatestaticasync Task<string> GetWebPageAsync(string url)
{
// Start an async task.
Task<string> getStringTask = (new HttpClient()).GetStringAsync(url);
// Await the task. This is what happens:
// 1. Execution immediately returns to the calling method, returning a
// different task from the task created in the previous statement.
// Execution in this method is suspended.
// 2. When the task created in the previous statement completes, the
// result from the GetStringAsync method is produced by the Await
// statement, and execution continues within this method.
Debug.WriteLine("In GetWebPageAsync before await");
string webText = await getStringTask;
Debug.WriteLine("In GetWebPageAsync after await");
return webText;
}
// Output:
// In GetWebPageAsync before await
// In startButton_Click before await
// In GetWebPageAsync after await
// Characters received: 44306
}
The error message is exactly right: the Main() method cannot be async, because when Main()returns, the application usually ends.
If you want to make a console application that uses async, a simple solution is to create an asyncversion of Main() and synchronously Wait() on that from the real Main():
staticvoid Main()
{
MainAsync().Wait();
}
staticasync Task MainAsync()
{
// your async code here
}
This is one of the rare cases where mixing await and Wait() is a good idea, you shouldn‘t usually do that.
I copied below code from this link.But when I am compiling this code I am getting an entry point cannot be marked with the ‘async‘ modifier. How can I make this code compilable?
classProgram{static async voidMain(string[] args){Task<string> getWebPageTask =GetWebPageAsync("http://msdn.microsoft.com");Debug.WriteLine("In startButton_Click before await");string webText = await getWebPageTask;Debug.WriteLine("Characters received: "+ webText.Length.ToString());}privatestatic async Task<string>GetWebPageAsync(string url){// Start an async task. Task<string> getStringTask =(newHttpClient()).GetStringAsync(url);// Await the task. This is what happens: // 1. Execution immediately returns to the calling method, returning a // different task from the task created in the previous statement. // Execution in this method is suspended. // 2. When the task created in the previous statement completes, the // result from the GetStringAsync method is produced by the Await // statement, and execution continues within this method. Debug.WriteLine("In GetWebPageAsync before await");string webText = await getStringTask;Debug.WriteLine("In GetWebPageAsync after await");return webText;}// Output: // In GetWebPageAsync before await // In startButton_Click before await // In GetWebPageAsync after await // Characters received: 44306}