为什么以下async而await不起作用?我试图了解这是想了解我的代码有什么问题.
class Program
{
static void Main(string[] args)
{
callCount();
}
static void count()
{
for (int i = 0; i < 5; i++)
{
System.Threading.Thread.Sleep(2000);
Console.WriteLine("count loop: " + i);
}
}
static async void callCount()
{
Task task = new Task(count);
task.Start();
for (int i = 0; i < 3; i++)
{
System.Threading.Thread.Sleep(4000);
Console.WriteLine("Writing from callCount loop: " + i);
}
Console.WriteLine("just before await");
await task;
Console.WriteLine("callCount completed");
}
}
Run Code Online (Sandbox Code Playgroud)
程序将启动count()方法,但在没有完成的情况下退出.随着等待任务; 语句我希望它在退出之前等待完成count()方法的所有循环(0,1,2,3,4).我只得到"计数循环:0".但是它经历了所有的callCount().它就像等待任务一样没有做任何事情.我希望count()和callCount()都异步运行并在完成时返回main.
我在 Visual Studio 中使用 Mapster 和 C#,并且需要将三个对象映射到一个对象。有人这样做过吗,有例子吗?Mapster 说它有这种能力,但我无法让它工作。谢谢。