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}

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

  1. Control Flow in Async Programs

    Control Flow in Async Programs You can write and maintain asynchronous programs more easily by using ...

  2. Dart 基础重点截取 Dart 2 20180417

    官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...

  3. CLR via C# 3rd - 08 - Methods

       Kinds of methods        Constructors      Type constructors      Overload operators      Type con ...

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

    直接Ctrl+F 搜索你要找的错 # # Simplified Chinese translation for subversion package # This file is distribute ...

  5. 9.Methods(二)

    4.Operator Overload Methods allow a type to define how operators should manipulate instances of the ...

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

    原文 : DotNetAnywhere: An Alternative .NET Runtime 作者 : Matt Warren 译者 : 张很水 我最近在收听一个名为DotNetRock 的优质播 ...

  7. C# to IL 12 Arrays(数组)

    An array is a contiguous block of memory that stores values of the same type. These valuesare an ind ...

  8. DotNetAnywhere

    DotNetAnywhere:可供选择的 .NET 运行时   原文 : DotNetAnywhere: An Alternative .NET Runtime作者 : Matt Warren译者 : ...

  9. SVN错误信息汇总

    svn错误信息  # # Simplified Chinese translation for subversion package # This file is distributed under ...

随机推荐

  1. C# 之 @ Assembly

    @ Assembly指定需要动态编译的类,在编译期间将程序集链接到 ASP.NET 应用程序页(例如网页.用户控件.母版页或 Global.asax 文件),使程序集的所有类和接口都在该页上可用. & ...

  2. 一起学Hadoop——实现两张表之间的连接操作

    ---恢复内容开始--- 之前我们都是学习使用MapReduce处理一张表的数据(一个文件可视为一张表,hive和关系型数据库Mysql.Oracle等都是将数据存储在文件中).但是我们经常会遇到处理 ...

  3. python_异常处理_断言

    一.Python标准异常 常用异常 Exception 常规错误的基类 AttributeError 试图访问一个对象没有的属性 IOError 输入/ 输出异常,基本上是无法打开文件 ImportE ...

  4. Bean Shell常用内置变量总结

    JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下: log:写入信息到jmeber.log文件,使用方法:log.info( ...

  5. FATAL ERROR: please install the following Perl modules before executing ./mysql_install_db: Data::Dumper

    今天安装本地数据库,所遇到的错误 FATAL ERROR: please install the following Perl modules before executing ./mysql_ins ...

  6. maven里面pom文件的各标签介绍

    由于maven在工作中经常使用,但是平时要记的知识点有点多,偶尔回头来看一些东西难免忘记,特此整理一篇笔记,方便大家搜索查询,也方便自己以后查询! 后续碰见其他的标签也会进行更新! maven的pom ...

  7. Ubuntu18.04上安装java

    安装java8 sudo add-apt-repository ppa:webupd8team/javasudo apt-get updatesudo apt-get install oracle-j ...

  8. Shiro笔记(三)shiroFilter拦截器配置原则

    参考: http://blog.csdn.net/yaowanpengliferay/article/details/17281341

  9. Maya cmds filterExpand 列出 选择的 uvs vertices faces edges 等 component 类型

    Maya cmds filterExpand 列出 选择的 uvs vertices faces edges 等 component 类型 cmds.ls() 的 flags 中没有指明 uvs 等这 ...

  10. mac安装Elasticsearch

    去官网下载https://www.elastic.co/downloads/elasticsearch 本人使用的是2.3.2版本,我下载的是tar版本的,然后解压. 进入es的安装目录, cd el ...