class PPSd
{
static void Main()
{
Console.WriteLine($"当前线程:{Thread.CurrentThread.ManagedThreadId}");
var ss = new Test().Haha();
Console.WriteLine($"curr:{Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine("最外层");
Thread.Sleep(1000);
Console.WriteLine("中间层");
Console.WriteLine($"结果:{ss.Result}");
Console.WriteLine("结束");
Console.Read();
}
}
public class Test
{
public async Task Haha()
{
Console.WriteLine("走起");
var dd = await ss();
Console.WriteLine("里走完外层1");
Console.WriteLine($"走上面await里面新开线程:{Thread.CurrentThread.ManagedThreadId}");
var dd2 = await ss();
Console.WriteLine("里走完外层2");
Console.WriteLine($"走上面await里面新开线程:{Thread.CurrentThread.ManagedThreadId}");
return dd;
}
public Task ss()
{
Thread.Sleep(4000);
Console.WriteLine($"走调用ss时的线程:{Thread.CurrentThread.ManagedThreadId}");
var dd= Task.Factory.StartNew(() =>
{
Thread.Sleep(5000);
Console.WriteLine($"走新线程:{Thread.CurrentThread.ManagedThreadId}");
return 1;
});
Console.WriteLine($"走调用ss时的线程:{Thread.CurrentThread.ManagedThreadId}");
return dd;
}
}
结果