为什么以下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"); } }程序继续启动count()方法,但未完成就退出.完成等待任务;我期望它在退出之前等待它完成count()方法的所有循环(0、1、2、3、4).我只得到计数循环:0".但是它正在经历所有的callCount().就像等待任务一样,它什么也没做.我希望count()和callCount()都异步运行,并在完成后返回main.
解决方案执行async方法时,它开始同步运行,直到到达await语句,然后其余代码异步执行,然后执行返回到呼叫者.
在您的代码中,callCount()开始与await task同步运行,然后返回到Main()方法,并且由于您不等待该方法完成,因此该程序将在方法count()无法完成的情况下结束.
通过将返回类型更改为Task,然后在Main()方法中调用Wait(),您可以看到所需的行为.
static void Main(string[] args) { callCount().Wait(); } static void count() { for (int i = 0; i < 5; i++) { System.Threading.Thread.Sleep(2000); Console.WriteLine("count loop: " + i); } } static async Task callCount() { Task task = new Task(count); task.Start(); for (int i = 0; i < 3; i++) { System.Threading.Thread.Sleep(1000); Console.WriteLine("Writing from callCount loop: " + i); } Console.WriteLine("just before await"); await task; Console.WriteLine("callCount completed"); }这是您的代码执行的方式:
(为更好地理解,让更改CallCount()将返回类型更改为Task)
如果要等待CallCount()中的计数任务完成而没有返回Main()方法,请调用task.Wait();,所有程序将等待Count(),但这不是await会执行的操作.
此链接详细介绍了异步等待模式. >
希望此代码工作流程图对您有所帮助.
Why is the following async and await not working? I am trying to learn this would like to understand what is wrong with my code.
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"); } }The program goes starts the count() method but drops out without completing it. With the await task; statement I was expecting it to wait to complete all loops of the count() method (0, 1, 2, 3, 4) before exiting. I only get "count loop: 0". But it is going through all of callCount(). Its like await task isn't doing anything. I want both count() and callCount() to run asynchronously and return to main when complete.
解决方案When you execute an async method, it starts running synchronously until it reaches an await statement, then the rest of the code executes asynchronously, and execution return to the caller.
In your code callCount() starts running synchronously to await task, then back to Main() method, and since you are not waiting for the method to complete, the program ends without method count() can finish.
You can see the desired behavior by changing the return type to Task, and calling Wait() in Main() method.
static void Main(string[] args) { callCount().Wait(); } static void count() { for (int i = 0; i < 5; i++) { System.Threading.Thread.Sleep(2000); Console.WriteLine("count loop: " + i); } } static async Task callCount() { Task task = new Task(count); task.Start(); for (int i = 0; i < 3; i++) { System.Threading.Thread.Sleep(1000); Console.WriteLine("Writing from callCount loop: " + i); } Console.WriteLine("just before await"); await task; Console.WriteLine("callCount completed"); }EDIT: This is how your code executes:
(for better understanding lets changes CallCount() return type to Task)
If you want to wait for count task finishes in CallCount() without returning to Main() method, call task.Wait();, all the program will wait for Count(), but this is not what await will do.
This link explains async-await pattern in details.
Hopes this workflow diagram of your code helps you.
更多推荐
C#异步和等待不起作用
发布评论