Is prevTask.Wait() recommended to be used with ContinueWith (from the Tasks library)?

前端 未结 7 1330
独厮守ぢ
独厮守ぢ 2020-12-07 15:40

So I was told recently that how I was using my .ContinueWith for Tasks was not the proper way to use them. I have yet to find evidence of this on the internet so I will ask

相关标签:
7条回答
  • 2020-12-07 16:15

    Who told you that?

    Quoting MSDN:

    Creates a continuation that executes asynchronously when the target Task completes.

    Also, what would be the purpose of Continue With if it wasn't waiting for the previous task to be completed?

    You can even test it by yourself:

    Task.Factory.StartNew(() =>
        {
            Console.WriteLine("Step 1");
            Thread.Sleep(2000);
        })
        .ContinueWith((prevTask) =>
        {
            Console.WriteLine("I waited step 1 to be completed!");
        })
        .ContinueWith((prevTask) =>
        {
            Console.WriteLine("Step 3");
        });
    
    0 讨论(0)
提交回复
热议问题