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. Windows下80端口被进程System占用的解决方法

    最近电脑时不时就发生了80端口被占用的情况,简单百度解决后,当重启电脑的时候又发生被占用的情况.今天非常幸运的是,发生了80端口和8080端口都被占用了情况,忍无可忍决定下定决心解决这个坑爹的问题,经 ...

  2. 从源码开始运行Bitcoin Core

    安装Ubuntu 环境:虚拟机 网络连接:桥接 系统版本:16.04 源:ali 安装编译环境(依赖库) sudo apt-get update sudo apt-get install build- ...

  3. WPF中datagrid不显示滚动条问题

    WPF中DataGrid是自带滚动条的,当内容显示不下时可以使用滚动条显示.但是,开发中遇到了DataGrid一直不显示滚动条.之前也曾遇到过同类问题,再次经过查找,发现是StackPanel的原因, ...

  4. ionic2集成sdk后,连接超时的问题

    这几天在测试app的时候,偶尔会发现集成的好视通sdk在进入会议室的时候出现连接超时的问题,一直卡在‘正在获取版本信息’的页面,经反复测试后这种现象主要出现在点击返回按钮和退出登录按钮的时候会出现,也 ...

  5. 谈谈Nginx-HTTPS加密技术

    超文本传输安全协议(HTTPS)是以安全为目标的HTTP通道,简单来说就是HTTP安全版.https由两个部分组成:HTTP+SSL/TLS,在http基础上加上了一层加密信息模块,服务端和客户端的信 ...

  6. Spring Boot之 Controller 接收参数和返回数据总结(包括上传、下载文件)

            一.接收参数(postman发送) 1.form表单 @RequestParam("name") String name 会把传递过来的Form表单中的name对应 ...

  7. Django1.11.7配置静态文件

    Django配置静态文件分为三步 1.建文件夹 2.设置setting 3.页面引用 1.文件目录结构 在APP下新建static文件夹,将js和css文件放入文件夹 2.配置settings.py ...

  8. day15 函数的使用方法:递归函数

    这里归纳的知识点主要就是: 浮点数,复数, int,整形:float,浮点数:complx,复数: # 实数: # 有理数 有限小数 1.2 # 无限循环小数 1.23232323232323.... ...

  9. TMS320DM642学习----第二篇(软件环境搭建+工程文件结构)

    一.CCS3.3安装: 下载解压CCS3.3的破解安装包如下所示,双击setup准备安装(SEED开发者论坛资料:http://www.seeddsp.com/bbs/forum.php?mod=vi ...

  10. 005.Ceph文件系统基础使用

    一 Ceph文件系统 1.1 概述 CephFS也称ceph文件系统,是一个POSIX兼容的分布式文件系统. 实现ceph文件系统的要求: 需要一个已经正常运行的ceph集群: 至少包含一个ceph元 ...