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 async
version 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?
|
An entry point cannot be marked with the 'async' modifier的更多相关文章
- Control Flow in Async Programs
Control Flow in Async Programs You can write and maintain asynchronous programs more easily by using ...
- Dart 基础重点截取 Dart 2 20180417
官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...
- CLR via C# 3rd - 08 - Methods
Kinds of methods Constructors Type constructors Overload operators Type con ...
- 【转】 svn 错误 以及 中文翻译
直接Ctrl+F 搜索你要找的错 # # Simplified Chinese translation for subversion package # This file is distribute ...
- 9.Methods(二)
4.Operator Overload Methods allow a type to define how operators should manipulate instances of the ...
- [搬运] DotNetAnywhere:可供选择的 .NET 运行时
原文 : DotNetAnywhere: An Alternative .NET Runtime 作者 : Matt Warren 译者 : 张很水 我最近在收听一个名为DotNetRock 的优质播 ...
- C# to IL 12 Arrays(数组)
An array is a contiguous block of memory that stores values of the same type. These valuesare an ind ...
- DotNetAnywhere
DotNetAnywhere:可供选择的 .NET 运行时 原文 : DotNetAnywhere: An Alternative .NET Runtime作者 : Matt Warren译者 : ...
- SVN错误信息汇总
svn错误信息 # # Simplified Chinese translation for subversion package # This file is distributed under ...
随机推荐
- 如何在grails2.3.x中的fork模式下进行调试?-【grails】
grails2.3.x中默认情况下运行模式被设置成了fork模式,在这种模式下,大家会发现设置了断点后无法进行中断.这是由于fork模式造成的,因为在fork模式下,JVM新起了一个进程,这样调试器就 ...
- ionic2+中修改minSdkVersion的方法
具体方法很简单,直接在config.xml中找到下面这一行 <preference name="android-minSdkVersion" value="17&q ...
- Flask Web中用MySQL代替SQLite
由于.sqlite文件操作的诸多不便,决定常识用MySQL代替书上的SQLite作为数据库. 1.在虚拟环境中安装mysql-Python具体步骤为: <1> 安装python-dev s ...
- 大数据——hbase
进入hbase hbase shell 部分命令清单 查询服务器状态 status 查询hbase版本 version 1. 创建一个表 create 'table1', 'tab1_id', ...
- 005 使用SpringMVC开发restful API三--处理创建请求
一:主要任务 1.说明 @RequestBody 映射请求体到java方法的参数 日期类型参数的处理 @Valid注解 BindingResult验证请求参数的合法性并处理校验结果 二:@Reques ...
- BZOJ BLO 1123 (割点)【双连通】
<题目链接> 以下内容转自李煜东的<算法竞赛进阶指南> 题目大意:现在给定一张连通的无向图,不包含重边.现在输出$n$个整数,表示将第$i$个节点的所有与其它节点相关联的边去掉 ...
- Mysql:数据库导入导出
Mysql:数据库导入导出 Mysql数据库导出 mysqldump -h IP -u 用户名 -p 数据库名 > 导出的文件名 1.mysqldump是在cmd下的命令,需要在linux命令行 ...
- 并发编程之I/O模型
1.I/O模型介绍 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个 ...
- 南阳171----聪明的kk
//简单的dp #include<cstdio> #define Max(a,b) ((a)>(b)?(a):(b)) ]; int main() { int i,j,n,m,x,t ...
- php 通用数据库类
<?php // 数据库连接类 class DB{ //私有的属性 private static $dbcon=false; private $host; private $port; priv ...