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
{
    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


28down votefavorite

5

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}

c# async-await c#-5.0

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

时间: 2024-10-13 06:04:22

An entry point cannot be marked with the 'async' modifier的相关文章

【转】 svn 错误 以及 中文翻译

直接Ctrl+F 搜索你要找的错 # # Simplified Chinese translation for subversion package # This file is distributed under the same license as the subversion package. # # Update to new pot: # msgmerge --update zh_CN.po subversion.pot # # Check translation: # msgfmt

WebKit 中异步加载脚本(Running scripts in WebKit)- 大大提升界面呈现速度

WebKit 中异步加载脚本(Running scripts in WebKit)- 大大提升界面呈现速度 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. Running scripts in

SVN中文提示

# # Simplified Chinese translation for subversion package # This file is distributed under the same license as the subversion package. # # Update to new pot: #    msgmerge --update zh_CN.po subversion.pot # # Check translation: #    msgfmt --statisti

[搬运] DotNetAnywhere:可供选择的 .NET 运行时

原文 : DotNetAnywhere: An Alternative .NET Runtime 作者 : Matt Warren 译者 : 张很水 我最近在收听一个名为DotNetRock 的优质播客,其中有以Knockout.js而闻名的Steven Sanderson 正在讨论 " WebAssembly And Blazor ". 也许你还没听过,Blazor 正试图凭借WebAssembly的魔力将 .NET 带入到浏览器中.如果您想了解更多信息,Scott Hanselme

(译)Asynchronous programming and Threading in C# (.NET 4.5)

原文地址:http://www.codeproject.com/Articles/996857/Asynchronous-programming-and-Threading-in-Csharp-N 介绍: Asynchronous programming and threading is very important feature for concurrent or parallel programming. Asynchronous programming may or may not us

Task-based Asynchronous Operation in WCF z

Download source - 93.5 KB Introduction Though performance blocking and sluggishness are the tailbacks for any application, we can easily overcome these bottlenecks by using asynchronous programming. But old-style practice for asynchronous programming

领域驱动设计实践上篇

一.前言 领域驱动设计的概念最早是由著名的建模专家Eric Evans在2004年发表的著名书籍 Domain-Driven Design –Tackling Complexity in the Heart of Software(中文译名:领域驱动设计 2006年3月清华出版社译本,或称 Domain Driven-Design architecture [Evans DDD]).园子里有很多人早已将其实践并应用,关于其要素.特点不再赘述,很多人在技术选型时想用它但又怕驾驭不了它,无非是没有真

FOUNDATION OF ASYNCHRONOUS PROGRAMMING

The async and await keywords are just a compiler feature. The compiler creates code by using the Task class. Instead of using the new keywords, you could get the same functionality with C# 4 and methods of the Task class; it's just not as convenient.

c# async

• Methods (as well as lambda expressions or anonymous methods) can be marked with the async keyword to enable the method to do work in a nonblocking manner.• Methods (as well as lambda expressions or anonymous methods) marked with the async keyword w