views:

46

answers:

1

Hi guys,

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

+1  A: 

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.

Jon Skeet
The details started here, but it was a different question: http://stackoverflow.com/questions/3447189/c-multiple-sources-different-threads-one-event-handler ... I will try to edit my question
Cristi
Hi Jon, thank you very much! You have a very good article there. Because I reached already a higher level of complexity in my code (for plugins communication)... I would've liked something more lightweight at the surface to use. That's why I was looking for something like Dispatcher. Do you know, is this working for windows services? Do you know how I can manipulate it during service start/stop?
Cristi
How can I marshal from one thread to another in .net 2.0 (for windows service)?
Cristi
I picked your answer since I couldn't use Dispatcher in the windows service ('dispatcher.Invoke' call would hang there)... so thanks for the alternative.
Cristi