是否有机会用 TPL 替换 while 循环?

发布于 2024-09-25 13:59:47 字数 759 浏览 4 评论 0原文

我有两个 while 循环,它们将与 TPL 并行运行。

我的代码:

public void Initialize()
{
   cts = new CancellationTokenSource();

   ParallelOptions options = new ParallelOptions();
   options.CancellationToken = cts.Token;
   options.MaxDegreeOfParallelism = Environment.ProcessorCount;

    task = Task.Factory.StartNew(() => Parallel.Invoke(options, Watcher1, Watcher2), cts.Token);
}

public void Watcher1()
{
   //Can I replace this (with a TPL construct in the initialize method)?
   while(true)
   {
      //Do sth.
   }
}

public void Watcher2()
{
   //Can I replace this (with a TPL construct in the initialize method)?
   while(true)
   {
      //do sth 
   }
}

如果我可以安全地取消这两个操作,那就太好了。你能给我一些建议吗?

提前致谢。

亲切的问候,亲

I've a two while loops, which I will have run parallel with the TPL.

My code :

public void Initialize()
{
   cts = new CancellationTokenSource();

   ParallelOptions options = new ParallelOptions();
   options.CancellationToken = cts.Token;
   options.MaxDegreeOfParallelism = Environment.ProcessorCount;

    task = Task.Factory.StartNew(() => Parallel.Invoke(options, Watcher1, Watcher2), cts.Token);
}

public void Watcher1()
{
   //Can I replace this (with a TPL construct in the initialize method)?
   while(true)
   {
      //Do sth.
   }
}

public void Watcher2()
{
   //Can I replace this (with a TPL construct in the initialize method)?
   while(true)
   {
      //do sth 
   }
}

It would be nice, if I can cancel this two actions safely. Can you give me some advices?

Thanks in advance.

Kind regards, pro

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

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

发布评论

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

评论(2

匿名。 2024-10-02 13:59:47
public void Initialize()
{
   var cancellationSource = new CancellationTokenSource();
   var cancellationToken = cancellationSource.Token;

   //Start both watchers
   var task1 = Task.Factory.StartNew(() => Watcher1(cancellationToken));
   var task2 = Task.Factory.StartNew(() => Watcher2(cancellationToken));

   //As an example, keep running the watchers until "stop" is entered in the console window
   while (Console.Readline() != "stop")
   {
   }

   //Trigger the cancellation...
   cancellationSource.Cancel();

   //...then wait for the tasks to finish.
   Task.WaitAll(new [] { task1, task2 });
}

public void Watcher1(CancellationToken cancellationToken)
{
   while (!cancellationToken.IsCancellationRequested)
   {
      //Do stuff
   }
}

public void Watcher2(CancellationToken cancellationToken)
{
   while (!cancellationToken.IsCancellationRequested)
   {
      //Do stuff
   }
}
public void Initialize()
{
   var cancellationSource = new CancellationTokenSource();
   var cancellationToken = cancellationSource.Token;

   //Start both watchers
   var task1 = Task.Factory.StartNew(() => Watcher1(cancellationToken));
   var task2 = Task.Factory.StartNew(() => Watcher2(cancellationToken));

   //As an example, keep running the watchers until "stop" is entered in the console window
   while (Console.Readline() != "stop")
   {
   }

   //Trigger the cancellation...
   cancellationSource.Cancel();

   //...then wait for the tasks to finish.
   Task.WaitAll(new [] { task1, task2 });
}

public void Watcher1(CancellationToken cancellationToken)
{
   while (!cancellationToken.IsCancellationRequested)
   {
      //Do stuff
   }
}

public void Watcher2(CancellationToken cancellationToken)
{
   while (!cancellationToken.IsCancellationRequested)
   {
      //Do stuff
   }
}
打小就很酷 2024-10-02 13:59:47

您可以使用 CancellationTokenSource 来完成此操作,请参阅这篇文章了解详情

You can do it using a CancellationTokenSource, see this article for details

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文