它给了我构建错误(我正在使用vs 2017),但在错误列表中没有找到错误
public static async void Main(string[] args)
{
await LongOP1();
}
public static async Task LongOP1()
{
long x = 0;
await Task.Run(() =>
{
for (int i = 0; i <= 10000; i++)
{
for (int j = 0; j <= 10000; j++)
{
x += i + j;
}
}
});
}
答案 0 :(得分:2)
您不能在Main方法上使用async关键字。
请参阅此替代方案,并查看该主题中的其他答案以获得解释: https://stackoverflow.com/a/24601591/4587181
相关代码:
static void Main(string[] args)
{
Task.Run(async () =>
{
// Do any async anything you need here without worry
}).GetAwaiter().GetResult();
}
答案 1 :(得分:0)
我更喜欢这样做
public static void Main()
{
Task t = LongOP1();
// Do other stuff here...
t.Wait();
}
public static async Task LongOP1()
{
long x = 0;
await Task.Run(() =>
{
for (int i = 0; i <= 10000; i++)
{
for (int j = 0; j <= 10000; j++)
{
x += i + j;
}
}
});
}