简体   繁体   English

如何停止所有C#任务在C#中的线程内安全运行

[英]How to stop all C# Tasks safely running inside a Thread in C#

I am having a C# thread which is running repeatedly after 10 minutes and from this thread suppose 10 C# 4.0 tasks are running for different amount of time doing some work. 我有一个C#线程,该线程在10分钟后会重复运行,并且从这个线程中假设10个C#4.0任务在执行一些工作的时间不同。

Now when I am repeating the thread how can I ensure that all the 10 inner tasks gets stopped safely and then start again once this thread gets start running again. 现在,当我重复该线程时,如何确保安全地停止所有10个内部任务,然后在该线程重新开始运行后再次启动。

If you want to wait until some Task s complete, you could store them somewhere and then call Task.WaitAll() . 如果要等到某些Task完成,可以将它们存储在某个地方,然后调用Task.WaitAll()

But it's hard to say if this is the best approach without knowing more about your code. 但是,如果不进一步了解您的代码,很难说这是否是最好的方法。

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

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