Threadpool v / s任务类 [英] Threadpool v/s Task classes

查看:61
本文介绍了Threadpool v / s任务类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


大家好,

Hi All,


我有三个工作项必须并行运行,我使用以下代码执行三个工作项,如下所示,这个工作项是不可阻挡的

I have three work items which has to be run parallelly, i am using follwoing code to perform three work items as shown below , this work items are unstoppable


XYZMethod()

XYZMethod()


{


ThreadPool.QueueUserWorkItem(new WaitCallback(ValidateInformation));

ThreadPool.QueueUserWorkItem(new WaitCallback(ValidateInformation));


  ThreadPool.QueueUserWorkItem(new WaitCallback(Traceprocessing));

 ThreadPool.QueueUserWorkItem(new WaitCallback(Traceprocessing));


  ThreadPool.QueueUserWorkItem(new WaitCallback(SendMessage));

 ThreadPool.QueueUserWorkItem(new WaitCallback(SendMessage));


}


OnElapsedTime()

OnElapsedTime()


{


XYZMethod();

XYZMethod();


}



使用Timer类每隔一分钟调用OnElapsedTime。 

The OnElapsedTime is called every one minute using Timer class. 


此代码是在我们运行.NET 3.5时开发的,但现在我们已经升级到4.0。现在我的问题是如何我可以用任务类替换Threadpool。

This code was developed while we are running with .NET 3.5 , but now we have upgraded to 4.0 . Now my question is how can i replace Threadpool with  Task classes.


由于我是TPL的新手,请帮助解决这个问题。

As i am new to the TPL , Please help on this issue.



Thansk提前

Thansk in advance


Mounesh

Mounesh

推荐答案

嗨Mounesh HB,

Hi Mounesh H B,

感谢您发布此处。

对于您的问题,线程池直到支持.net 4.0。您可以使用它。 net 4.0。

For your question, Thread Pool till support in .net 4.0. You could use it for .net 4.0.

如果你想使用4.0中的新功能来制作三个必须并行运行的工作项,那么最好使用并行类而不是任务。

If you want to use new features in 4.0 to make three work items which has to be run parallelly, it would be better to use parallel class instead of task.

有关并行的更多详细信息,请参阅MSDN文章。

For more details about parallel, you could refer to the MSDN article.

https://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel(v = VS。 110).aspx

https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-foreach-loop

最诚挚的问候,

Wendy


这篇关于Threadpool v / s任务类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆