简体   繁体   English

.net多线程

[英].net multi-threading

I'm writing dll which must run a function multiple times using different parameters, parallell to everything else. 我正在编写dll,它必须使用不同的参数(与其他所有参数并行)多次运行一个函数。

I've tried the following methods: 我尝试了以下方法:

  • Backgroundworkers Backgroundworkers
  • MethodInvoker MethodInvoker
  • Threads 主题
  • etc 等等

What's the best way to do it? 最好的方法是什么?

Update : 更新

I'm writing a dll, no GUI involved 我正在编写一个dll,不涉及GUI

If you're using .NET 4.0, you should make use of the new Parallel Task Library . 如果您使用的是.NET 4.0,则应使用新的并行任务库

If not, consider using the threadpool to do your work with ThreadPool.QueueUserWorkItem() . 如果没有,请考虑使用ThreadPool.QueueUserWorkItem()通过ThreadPool.QueueUserWorkItem() Failing that, creating a new Thread object is the way to go. 失败的话,创建一个新的Thread对象是Thread的方法。 Do not use BackgroundWorker for this; 不要使用BackgroundWorker这个; only if you were trying to decouple long-running processor-intensive code from blocking the UI would you use the BackgroundWorker . 仅当您尝试将长时间运行的处理器密集型代码与阻止UI分离开时,您才使用BackgroundWorker

There's a pretty good example of waiting for all the user workitems to complete here: http://msdn.microsoft.com/en-us/library/z6w25xa6.aspx . 这里有一个等待所有用户工作项完成的很好的例子: http : //msdn.microsoft.com/zh-cn/library/z6w25xa6.aspx Essentially you pass a ManualResetEvent object into each call that is Set() when the async code completes. 本质上,当异步代码完成时,您将ManualResetEvent对象传递给Set()每个调用中。 You put all the ManualResetEvent objects in an array and call WaitHandle.WaitAll() passing in that array to wait for all the waithandles to be set. 您将所有ManualResetEvent对象放入一个数组中,并调用传入该数组的WaitHandle.WaitAll()以等待所有waithandles的设置。

Another technique could be to have a controlled number of Thread objects running that are waiting for items to appear in a Queue . 另一种技术可能是运行受控数量的Thread对象,这些对象正在等待项目出现在Queue This is a fairly standard Producer/Consumer pattern that means you need less threads (and therefore resources) but can still decouple the request from the work by putting messages on a queue. 这是一个相当标准的生产者/消费者模式,这意味着您需要更少的线程(因此也需要资源),但是仍然可以通过将消息放在队列中来使请求与工作分离。 If you need the queue to be durable you could use MSMQ (and you can go even further than that with an ESB). 如果您需要持久的队列,则可以使用MSMQ(并且与ESB相比,您甚至可以走得更远)。

As you can see, where you are considering multi-threading makes a massive difference on how you implement it. 正如你所看到的, 在那里你正在考虑多线程使你如何实现它一个巨大的差异。 Windows Form multithreading would not be the same as a Windows Service's multithreading, and in turn neither would be the same as in an ASP.NET web application (where arguably threading isn't even a good idea due to resource management in IIS). Windows窗体多线程与Windows服务的多线程并不相同,反过来也与ASP.NET Web应用程序(由于IIS中的资源管理而使线程甚至不是一个好主意)相同。 If you update your question we can be more specific to your current needs. 如果您更新您的问题,我们可以更具体地满足您当前的需求。

Threading is tricky - which is "best" depends on the very specific scenario. 线程处理非常棘手-“最佳”取决于具体情况。 For example, on a web server "best" may be not to use threading at all , and let other threads handle other requests. 例如,在Web服务器上的“最佳”可能是不使用线程所有,并让其他线程处理其他请求。 One thing you should avoid is saturating the ThreadPool , as other core parts of the system depend on that. 您应该避免的一件事是使ThreadPool饱和,因为系统的其他核心部分都依赖于此。

It sounds like a multiple-reader worker queue would serve your purposes; 听起来多读者工作队列会满足您的目的; note also that .NET 4.0 introduces a range of controlls (around "Parallel" and "Tasks") that make threaded code easier to handle. 还请注意,.NET 4.0引入了一系列控件(在“并行”和“任务”周围),使线程代码更易于处理。 A reasonable overview is here . 这里有一个合理的概述。

My suggestions: 我的建议:

  • If you are starting a background thread from a WinForms UI, use the BackgroundWorker component. 如果要从WinForms UI启动后台线程,请使用BackgroundWorker组件。
  • If you don't particularly care when the job starts or finishes as long as it runs, use an asynchronous delegate or the thread pool. 如果您对作业的开始时间或完成时间不特别关注,请使用异步委托或线程池。
  • Otherwise, create a new thread manually. 否则,请手动创建一个新线程。

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

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