简体   繁体   English

Thread.Sleep在Task.Run中

[英]Thread.Sleep in Task.Run

I have been trying to get my head around C#'s new async/await and Task.Run functionality recently. 我最近试图了解C#的新async / await和Task.Run功能。 In doing so I wrote some simple test code that writes to the console so that I can see in what order things happen in, throwing in a Thread.Sleep here and there to make sure things really happens in the order I expected. 在这样做的过程中,我编写了一些简单的测试代码,这些代码写入控制台,以便我可以看到事情发生的顺序,在这里和那里投入Thread.Sleep以确保事情按照我预期的顺序发生。

Here is one of my tests 这是我的一个测试

[Test]
public void TaskRun()
{
    Console.WriteLine("Before");
    Task.Run(() => Console.WriteLine(_terminator.IWillBeBack()));
    Console.WriteLine("After");
}

with the following Terminator class: 使用以下Terminator类:

public class Terminator
{
    public string IWillBeBack()
    {
        return "I will be back";
    }
}

From this I expected the result to most likely be Before , After , and then I will be back . 由此我预计结果很可能是之前之后 ,然后我会回来 This is indeed what happens. 确实发生了这种情况。 Then I make a change to the IWillBeBack-method to allow it to sleep for 3 seconds. 然后我对IWillBeBack方法进行了更改,使其能够休眠3秒钟。 The code for this: 这个代码:

public class Terminator
{
    public string IWillBeBack()
    {
        Thread.Sleep(3000);
        return "I will be back";
    }
}

I expected the result to still be Before , After , and then after 3 seconds again I will be back . 我预计结果仍然是之前之后 ,然后再过 3秒后我会回来 This is not what happens though. 这不是发生的事情。 I instead get Before and After , but never I will be back . 我反而得到之前之后 ,但从来没有我会回来的

Why is this? 为什么是这样?

When I debug it clearly goes through the code, sleeping and returning. 当我调试它清楚地通过代码,睡觉和返回。 I don't need Thread.Sleep in this way in any production code, but I would like to have a vague understanding of why I will be back is not written to the console in the second case. 我不需要了Thread.Sleep以这种方式在任何生产代码,但我想有,为什么我会回来的一个模糊的认识不写入在第二种情况下,控制台。

Your test is completing before it tries to print "I will be back". 您的测试在尝试打印“我将回来”之前完成。 You're not doing anything to wait until that's finished. 你没有做任何事情等到那个结束。 Depending on the test runner, however it's redirecting Console.WriteLine may have been disconnected as soon as the test has finished. 根据测试运行器的不同,它会重定向Console.WriteLine可能在测试完成后立即断开连接。

If you want to see it finish, change your test to: 如果您想看到它完成,请将您的测试更改为:

[Test]
public void TaskRun()
{
    Console.WriteLine("Before");
    var task = Task.Run(() => Console.WriteLine(_terminator.IWillBeBack()));
    Console.WriteLine("After");
    task.Wait();
}

If you use the MS Test framework in VS 2012 you can also write the test like this 如果您在VS 2012中使用MS Test框架,您也可以像这样编写测试

[TestMethod]
public async Task TaskRun()
{
    Console.WriteLine("Before");
    Task t = Task.Run(() => Console.WriteLine(IWillBeBack()));
    Console.WriteLine("After");
    await t;
}

private string IWillBeBack()
{
    Thread.Sleep(3000);
    return "I will be back";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM