这是一个模仿我的实际案例流程的程序:
using System;
using System.Threading.Tasks;
namespace TestAsync
{
interface IInterface
{
Task<int> DoSomething(int i);
}
class MyClass : IInterface
{
public async void MainAsync()
{
var i = 1;
Console.WriteLine("Start MainAsync:" + i);
var t = DoSomething(i);
Console.WriteLine("After DoSomething: " + i );
i = await t;
Console.WriteLine("Done waiting: " + i);
}
public async Task<int> DoSomething(int i)
{
i = i + 1;
Console.WriteLine("In Something:" + i);
await Task.Delay(1000);
Console.WriteLine("After Long Process: " + i);
return i;
}
} …Run Code Online (Sandbox Code Playgroud)