代码输出如下:
0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
我应该把输出混合成这样:
... 01234X5X678XX
我的意思是两个方法都应该异步启动,而不是在第一个完全完成后启动。在下面的代码中修改哪里来实现这个目标?
using System;
using System.Threading.Tasks;
namespace ConsoleApplication36
{
class Program
{
public static void CountForward()
{
for (int i = 0; i < 100; i++) { Console.Write(i); }
}
public static void PrintX()
{
for (int i = 0; i < 100; i++) { Console.Write("X"); }
}
public async Task RunAsync()
{
Task t1 = Task.Run(() => CountForward());
Task t2 = Task.Run(() => PrintX());
Task[] tasks = { t1, t2 };
await Task.WhenAll(tasks);
}
static void Main(string[] args)
{
Task t = new Program().RunAsync();
t.Wait();
}
}
}