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
|
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 ...
随机推荐
- UIImageView的常用方法
//初始化 init(image: UIImage!) @availability(iOS, introduced=3.0)//初始化,highlightedImage 高亮图片 init(image ...
- C++ 语法--变量和常量
起步 Hello world! #include <iostream> int main() { std::cout<<"Hello, world!"; ; ...
- jQuery滑动开关按钮效果
效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- Vim的合并行操作
日常常用到多行合并的功能,记录如下: 第一种, 多行合并成一行,即: AAAAABBBBBCCCCC 合并为:AAAAA BBBBB CCCCC 方法1: normal状态下 3J 其中的3是范围,可 ...
- Codeforces 803G Periodic RMQ Problem 线段树
Periodic RMQ Problem 动态开点线段树直接搞, 我把它分成两部分, 一部分是原来树上的, 一部分是后来染上去的,两个部分取最小值. 感觉有点难写.. #include<bits ...
- UOJ#23. 【UR #1】跳蚤国王下江南 仙人掌 Tarjan 点双 圆方树 点分治 多项式 FFT
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ23.html 题目传送门 - UOJ#23 题意 给定一个有 n 个节点的仙人掌(可能有重边). 对于所有 ...
- NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序
原文链接https://www.cnblogs.com/zhouzhendong/p/9258043.html 题目传送门 - 洛谷P3953 题目传送门 - Vijos P2030 题意 给定一个有 ...
- TCP/UDP 协议,和 HTTP、FTP、SMTP,区别及应用场景
一.OSI 模型 OSI 模型主要作为一个通用模型来做理论分析,而TCP/IP 协议模型是互联网的实际通讯协议,两者一般做映射分析,以下不做严格区分和声明(好吧,比较懒): OSI 整个模型层次大致可 ...
- oracle的存储过程和定时任务编写体会
create or replace procedure clear_product is p_sql char(200) p_r_sql char(200) begin p_sql := ''; p_ ...
- TensorFlow激活函数+归一化-函数
激活函数的作用如下-引用<TensorFlow实践>: 这些函数与其他层的输出联合使用可以生成特征图.他们用于对某些运算的结果进行平滑或者微分.其目标是为神经网络引入非线性.曲线能够刻画出 ...