Thread.Sleep“中断”任务

发布于 2024-12-18 20:31:12 字数 511 浏览 2 评论 0原文

所以我有以下代码

 Action d = () =>
                {                                                             
                   for (int i = 0; i <= 10; i++)
                   {
                      Thread.Sleep(50);
                      Console.WriteLine("Task: {0} log:{1}",Thread.CurrentThread.ManagedThreadId,i);
                   }
                };
 Task.Factory.StartNew(d);

但是它没有输出任何内容。但如果我评论 Thread.Sleep,它会按预期工作。使用不同的睡眠值会根据该值获得或多或少的结果。

为什么会这样呢?

So I have the following code

 Action d = () =>
                {                                                             
                   for (int i = 0; i <= 10; i++)
                   {
                      Thread.Sleep(50);
                      Console.WriteLine("Task: {0} log:{1}",Thread.CurrentThread.ManagedThreadId,i);
                   }
                };
 Task.Factory.StartNew(d);

However it doeasn't output anything. But if I comment Thread.Sleep, it works as expected. Playing with different Sleep values gets me more or less results depending on the value.

Why does it happen this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小草泠泠 2024-12-25 20:31:12

我怀疑你的程序在任务有机会运行之前就退出了。

默认情况下,任务在线程池线程上运行,这些线程是后台线程。这意味着您的程序不会等待它们完成就退出。

尝试将其添加到您的主线程上:

var task = Task.Factory.StartNew( d );
task.Wait();

I suspect your program is exiting before the task has a chance to run.

Tasks run on thread pool threads by default, which are background threads. That means your program doesn't wait for them to finish before exiting.

Try adding this on your main thread:

var task = Task.Factory.StartNew( d );
task.Wait();
可是我不能没有你 2024-12-25 20:31:12

您是否正在等待任务完成,或者您的程序是否在任务完成之前退出?

Task task = Task.Factory.StartNew(d);
task.Wait();

Are you waiting for the task to finish anywhere, or does your program quit before the task is done?

Task task = Task.Factory.StartNew(d);
task.Wait();
过度放纵 2024-12-25 20:31:12

我刚刚运行您的代码,它输出:

Task: 10 log:0
Task: 10 log:1
Task: 10 log:2
Task: 10 log:3
Task: 10 log:4
Task: 10 log:5
Task: 10 log:6
Task: 10 log:7
Task: 10 log:8
Task: 10 log:9
Task: 10 log:10

这是基于我运行的代码应用程序的控制台代码:

using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      Action d = () =>
      {
        for (int i = 0; i <= 10; i++)
        {
          Thread.Sleep(50);
          Console.WriteLine("Task: {0} log:{1}", Thread.CurrentThread.ManagedThreadId, i);
        }
      };
      Task.Factory.StartNew(d);
      Console.ReadLine();
    }
  }
}

I've just run your code and it outputs:

Task: 10 log:0
Task: 10 log:1
Task: 10 log:2
Task: 10 log:3
Task: 10 log:4
Task: 10 log:5
Task: 10 log:6
Task: 10 log:7
Task: 10 log:8
Task: 10 log:9
Task: 10 log:10

Here is the code of console based on your code app I've run:

using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      Action d = () =>
      {
        for (int i = 0; i <= 10; i++)
        {
          Thread.Sleep(50);
          Console.WriteLine("Task: {0} log:{1}", Thread.CurrentThread.ManagedThreadId, i);
        }
      };
      Task.Factory.StartNew(d);
      Console.ReadLine();
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文