为何“等待”是不是在等待? [英] Why "await" is not awaiting?

查看:117
本文介绍了为何“等待”是不是在等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用RunTest()方法来执行测试。

我期待,它应该打印..

AsyncAndAwait start ...

做其他事情......

然后它应该等待一段时间..然后它应该打印我已经完成!!!



但它没有这样做..实际结果是。

AsyncAndAwait开始...

等待一段时间

做别的事......

我完成了!



请帮帮我了解它是如何工作的。





这是代码示例。



  async 任务< List< int>> Counting()
{
List< int> nums = new List< int>();
for int i = 0 ; i < 100 ; i ++)
{
Thread.Sleep ( 100 );
nums.Add(i);
}
返回 nums;

}


public async void AsyncAndAwait()
{
Console.WriteLine( AsyncAndAwait start ...);
var task = Counting();
Console.WriteLine( 做其他事情......);


var list = await 任务;
Console.WriteLine( 我完成了!!!);
}

public void RunTest()
{
AsyncAndAwait();
}

解决方案

阅读与await的异步不起作用。为什么? [ ^ ]


以下是我们应该编写的代码,以满足您的条件:



 async任务< List< int>> Counting()
{
Console.WriteLine(....其他......开始);
List< int> nums = new List< int>();
for(int i = 0; i< 100; i ++)
{
// Thread.Sleep(100);
await Task.Delay(100);
nums.Add(i);
}
Console.WriteLine(......别的......完成);
返回nums;
}
public async void AsyncAndAwait()
{
Console.WriteLine(AsyncAndAwait starting ...);
任务< List< int>> task = Counting();
Console.WriteLine(做其他事......);
List< int> list = task.Result;
Console.WriteLine(我做完!!!);
}

public void RunTest()
{
AsyncAndAwait();
}





我可以看到你的代码没有给出所需行为的几个原因:



1.在声明方法时只是放异步不会使它成为异步方法。必须等待它内部。如 MSDN页所述:如果async关键字修改的方法不包含等待表达式或语句,该方法同步执行。这就是为什么而不是Thread.Sleep(100)我使用await Task.Delay(100)。



2.只需编写var task = Counting (),你在同一个线程上调用Counting方法。如果你不使用await关键字,那么它就是一个阻塞调用。如 MSDN页面所述:await表达式不会阻止它执行的线程。欲了解更多信息,请访问:



http://stackoverflow.com/questions/23022573/calling-async-methods-from-a-synchronous-context [ ^ ]



希望它会有所帮助。感谢。


I am calling RunTest() method to execute the test.
I am expecting, it should print..
"AsyncAndAwait starting..."
"Doing something else..."
and then it should wait for some time.. and then it should print "I am done!!!"

But it not doing so.. The actual results are.
"AsyncAndAwait starting..."
Wait for some time
"Doing something else..."
"I am done!!!"

Please help me understanding how is it working.


Here is the code sample.

async Task<List<int>> Counting()
 {
     List<int> nums = new List<int>();
     for (int i = 0; i < 100; i++)
     {
         Thread.Sleep(100);
         nums.Add(i);
     }
     return nums;

 }


 public async void AsyncAndAwait()
 {
     Console.WriteLine("AsyncAndAwait starting...");
     var task = Counting();
     Console.WriteLine("Doing something else...");


     var list = await task;
     Console.WriteLine("I am done!!!");
 }

 public void RunTest()
 {
     AsyncAndAwait();
 }

解决方案

Read Async with await do not work. Why?[^]


Following is the code which we should be writing to fulfill your conditions:

async Task<List<int>> Counting()
{
    Console.WriteLine("....something else...Started");
    List<int> nums = new List<int>();
    for (int i = 0; i < 100; i++)
    {
        // Thread.Sleep(100);
        await Task.Delay(100);
        nums.Add(i);
    }
    Console.WriteLine(".....something else...Done");
    return nums;
}
public async void AsyncAndAwait()
{
    Console.WriteLine("AsyncAndAwait starting...");
    Task<List<int>> task = Counting();
    Console.WriteLine("Doing something else...");
    List<int> list = task.Result;
    Console.WriteLine("I am done!!!");
}

public void RunTest()
{
    AsyncAndAwait();
}



I can see few reasons why your code is not giving desired behaviors:

1. Just putting async while declaring method does not make it asynchronous method. There must be await inside it. As stated at MSDN page : "If the method that the async keyword modifies doesn't contain an await expression or statement, the method executes synchronously." That's why instead of "Thread.Sleep(100)" I used "await Task.Delay(100)".

2. Just by writing "var task = Counting()", you are calling "Counting" method on same thread. if you will not use await keyword it is a blocking call. As stated at MSDN page : "An await expression does not block the thread on which it is executing." For more please visit:

http://stackoverflow.com/questions/23022573/calling-async-methods-from-a-synchronous-context[^]

Hope it will help. Thanks.


这篇关于为何“等待”是不是在等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆