【发布时间】:2014-09-01 14:20:38
【问题描述】:
我在尝试一些 async 方法时收到以下错误。我正在关注这个例子:Microsoft Async Example
'await' 运算符只能在异步方法中使用
'await' 的方法是 IS async,我认为它与 Microsoft 示例几乎相同。
我在这里做错了什么?
按钮点击
private void btn_Async_Click(object sender, EventArgs e)
{
GeneralFeatures gf = new GeneralFeatures();
Task<long> getLongRunningData = gf._Async();
long answer = await getLongRunningData ;
}
异步方法
class GeneralFeatures
{
public async Task<long> _Async()
{
///// LONG RUNNING TASK /////////
int count = 0;
int j = 1101000;
long a = 2;
while (count < j)
{
long b = 2;
int prime = 1;// to check if found a prime
while (b * b <= a)
{
if (a % b == 0)
{
prime = 0;
break;
}
b++;
}
if (prime > 0)
count++;
a++;
}
///// LONG RUNNING TASK /////////
return a;
}
}
【问题讨论】:
-
private void btn_Async_Click告诉我你在哪里将它定义为async。
标签: c# asynchronous