多线程服务,BackgroundWorker的VS线程池?线程、多线程、BackgroundWorker、VS

2023-09-02 21:50:32 作者:不服来砍老子

我有一个.NET 3.5的Windows服务。我测试的小应用程序启动它们,为300随机时间跨度为6500ms后只是睡觉线程。我对这个问题的各种问题。

I have a .NET 3.5 Windows Service. I'm testing with a small application that just sleeps threads after starting them, for random timespans of 300 to 6500ms. I have various questions about this issue.

的BackgroundWorker 真的打算使用刚刚的WinForms应用程序或这只是无稽之谈,它是如何准确地调整到这种效果呢? 在我读过有关线程池 S在this问题和这个。我不完全相信它有多少对我来说是一个问题,线程会在某处持续半秒钟,几秒钟之间。这是有足够的理由看别的地方? 我是最好的关闭只是创建后台线程我? Is BackgroundWorker really intended for use just in WinForms applications or is this just nonsense, how exactly is it tuned to this effect? I've read about ThreadPools in this question and this one. I'm not exactly sure how much it is a problem for me that the threads would last somewhere between half a second and a few seconds. Is this reason enough to look somewhere else? Am I best off just creating the background threads myself?

的真实生活服务将轮询数据库为未决请求列表,执行线程为每个这些请求(限定在一定量并​​发线程),如果在数据库中存在着​​一些数据中的每个线程将被检查,检索它,如果是的话,或者从一个流API下载,存储,并返回数据。该下载将是耗费时间最多的部分。

The real-life service will poll the database for a list of pending requests, execute threads for each of those requests (limited to a certain amount of concurrent threads) and each thread will be checking if some data exists in a database, retrieve it if it does, or download it from a streaming API, store it, and return that data. The downloading would be the part that consumes the most time.

我真的很喜欢这个问题,要回答的.NET 3.5框架,但如果有更好或更有效的方法.NET 4.0下做到这一点,我想了解他们。与更多信息链接也非常欢迎。

I would really like this question to be answered for .NET 3.5 Framework, but if there are better or more efficient ways to accomplish this under .NET 4.0, I would like to read about them too. Links with more info are also very much welcome.

推荐答案

的BackgroundWorker 的价值在于它能够提高其 ProgressChanged 并创建它的实例的线程上 RunworkerCompleted 事件。这使得它的非常的方便之处在于不支持自由线程程序。

The value in BackgroundWorker is that it can raise its ProgressChanged and RunworkerCompleted event on the thread that created its instance. Which makes it very convenient in programs that cannot support free threading.

有关此正常工作,然而需要注意, SynchronizationContext.Current 属性引用非默认同步提供。提供者负责编组呼叫从一个线程到另一个。 NET框架有两个提供商都是现成的: System.Windows.Forms.WindowsFormsSynchronizationContext System.Windows.Threading.DispatcherSynchronizationContext 。这些提供商分别WinForms和WPF处理同步。

For this to work properly, it is however required that the SynchronizationContext.Current property references a non-default synchronization provider. A provider which is responsible for marshaling calls from one thread to another. The .NET framework has two providers that are readily available: System.Windows.Forms.WindowsFormsSynchronizationContext and System.Windows.Threading.DispatcherSynchronizationContext. These providers handle synchronization for, respectively, Winforms and WPF.

有一个连接,WinForms和WPF是有一个线程问题,这两个类库。两个协议都实现图形用户界面和基于Windows的图形用户界面基本上是线程安全的。窗户窗户只能从创建它们的线程更新。换句话说,这些自定义同步提供之所以存在是因为有一个迫切需要他们。另外值得注意的是,他们采取的方法UI线程工作的优势工作。 UI线程在一个事件驱动的方式执行code,抽消息循环接收通知。同步提供者可以注入呼叫通过这一机制的事件处理程序。这绝不是偶然的。

There's a connection, Winforms and WPF are both class libraries that have a threading problem. Both implement GUIs and Windows based graphical user interfaces are fundamentally thread-unsafe. Windows windows can only be updated from the thread that created them. In other words, these custom synchronization providers exist because there's a dire need for them. Also notable is that they work by taking advantage of the way UI threads work. A UI thread executes code in an event driven way, pumping a message loop to receive notifications. The synchronization provider can inject calls to event handlers using this mechanism. This is no accident.

回到主题,Windows服务就没有这样的设施。它没有GUI和没有安装自定义同步提供。因此,的BackgroundWorker 没有提供的功能,就是在服务很有用。如果没有自定义同步提供程序,默认提供简单的运行在一个线程池线程的事件。这是没有用的,你还不如火从你的工作线程的事件。获取事件的另一个特定线程上运行是很难实现的,除非你重新创建消息循环机制或钩到的WinForms管道,并使用一个不可见的窗口,一个模拟的UI线程。这并不完全少见BTW。

Back on topic, a Windows service has no such facility. It doesn't have a GUI and doesn't install a custom synchronization provider. As such, BackgroundWorker provides no feature that's useful in a service. Without the custom synchronization provider, the default provider simply runs events on a threadpool thread. Which is not useful, you might as well fire the event from your worker thread. Getting events to run on another specific thread is very difficult to achieve, unless you recreate the message loop mechanism or hook into the Winforms plumbing and create a simulated UI thread using an invisible window. Which is not entirely uncommon btw.

 
精彩推荐
图片推荐