问题描述
我需要 Dispatcher
(.net 3.0)用于Windows服务(在.net 2.0中完成).您能给我一些想法如何实现类似目标或为我指出一些链接吗?
I need an alternative for Dispatcher
(.net 3.0) to use for a windows service (done in .net 2.0). Can you give me some idea how to achieve something like that or point me some links?
我知道调度程序后面有SynchronizationContext
,但是我不知道如何在服务中使用SynchronizationContext
.
I know that a dispatcher has a SynchronizationContext
behind, but I don't know how I can use a SynchronizationContext
into a service.
如果您认为我应该坚持使用Dispatcher
(.net 3.0)...如何操作它(OnServiceStop
,OnServiceStart
)
If you think that I should stick to the Dispatcher
(.net 3.0) ... how can I manipulate it (OnServiceStop
, OnServiceStart
)
已编辑:更多详细信息(另请参见... 此处)
edited:More details (see also...here)
想法是,我想在Windows服务中托管一些扩展/插件,这些扩展/插件将通过方法ExecuteCommand(type, params)
在彼此之间进行通信.
Idea is that I would like to host into my windows service some extensions/plugins which would communicate between each-other through a method ExecuteCommand(type, params)
.
如果此方法是从插件内部执行的,则此方法还会向服务引发一个事件,以便接收结果.每个插件都可以从其调用此方法ExecuteCommand
的地方拥有自己的线程,因此我想将所有调用收集并同步到一个线程(主服务线程)中,以便适当地返回结果.
This method also raises an event to the service in order to receive results if it was executed from inside the plugin. Each plugin could have its own thread from where it calls this method ExecuteCommand
so I would like to gather and synchronize all the calls into one thread (main service thread) in order to return the result appropriately.
这就是Dispatcher发挥作用的原因.但是我可能想在.net 2.0中拥有某些东西,或者您认为Dispatcher对我而言是好的?
This is why Dispatcher came into play. But I would like to have, maybe, something in .net 2.0 or do you think Dispatcher is good in my case?
谢谢.
推荐答案
Windows服务没有Dispatcher
之类的东西(或Windows窗体中的消息循环).如果要从一个线程编组到另一个线程,则目标"线程将必须运行其自己的某种消息循环.
Windows Services don't have anything like the Dispatcher
(or message loop in Windows Forms). If you want to marshal from one thread to another, the "target" thread will have to be running its own sort of message loop.
如果您可以向我们提供更多有关您要实现的目标的信息,那么可以更轻松地为您提供帮助.
If you could tell us more about what you're trying to achieve, it would make it easier to help you.
好吧,这听起来像是基本上想要一个生产者/消费者队列:一个线程等待,直到队列中存在某些东西,然后对其进行处理.生产者可以随时添加到队列中.
Okay, it sounds like basically want a producer/consumer queue: one thread waits until something is present in the queue, and processes it. Producers can add to the queue whenever they like.
我有一个非常简单地实现了生产者/消费者队列在我的线程教程中,但可能会有更高级的实现. (.NET 4使这变得容易,但是在.NET 2中却很难.)如果您执行我的实现,则需要考虑使其通用并添加终止条件. Joe Albahari还有您也应该查看的另一种实现方式.
I have a very simple implementation of a producer/consumer queue in my threading tutorial, but there may be more advanced implementations around. (.NET 4 makes this easy, but it's harder in .NET 2.) If you do take my implementation, you'll want to think about making it generic and adding termination conditions. Joe Albahari has another implementation you should look at, too.
这篇关于可以在.net 2.0上下文中使用的Dispatcher类(从.net 3.0开始)的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!