开发者

Alternative of Dispatcher class (from .net 3.0) to use in .net 2.0 context

开发者 https://www.devze.com 2023-01-11 15:12 出处:网络
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?

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?

I know that a dispatcher has a SynchronizationContext behind, but I don't know how I can use a Synchro开发者_高级运维nizationContext into a service.

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)

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).

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.

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?

Thanks.


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.

EDIT: 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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消