嘗試一些async
方法時,出現以下錯誤。我下面這個例子:Microsoft Async Example異步/等待方法的錯誤
「等待」經營者只能異步方法
方法的「等待」是爲非同步內使用,我認爲這幾乎是一樣的微軟的例子。
我在做什麼錯在這裏?
按鈕單擊
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'的位置。 – 2014-09-01 14:23:55