An entry point cannot be marked with the 'async' modifier

本文介绍了一个C#程序中关于async修饰符在Main方法中的使用错误,并提供了解决方案。通过将Main方法分为同步和异步两个版本,可以有效避免入口点不能标记为async的问题。

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
{
    static async void 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()); 
    }

    private static async 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():

static void Main()
{
    MainAsync().Wait();
}

static async 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.

 

from:http://stackoverflow.com/questions/16712172/an-entry-point-cannot-be-marked-with-the-async-modifier

from:http://www.itstrike.cn/Question/f33637bc-2f7f-47b3-9985-0fe709b24d57.html

 

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}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值