|
Sorry for the cross post but this doesn't fit neatly into Silverlight/WPF!
I am asking this in the WPF forum because this is a WPF app but this question does not necessarily pertain to WPF, i.e. the UI thread.
Here is the scenario:
I have three (3) threads. Thread One (1) call it the Master Thread; Thread Two (2) is a Ethernet Comm thread; and Thread Three (3) is a GPIB comm thread. Both communication threads handle asynchronous communications from remote entities. At this time there is only one entity hanging off each comm type.
I have implemented a mix of EventWaitHandles and delegates. This all works pretty well. Just think this architecture is a bit ...... ugly. So which one is better/safer?
My concern is as follows. With delegates, for example, the Master thread registers an event handler with Thread 2 and an event handler with Thread 3 for messages that come in. Obviously data could come in near simultaneously. So Thread 2 encases a message in an EventArg and invokes the event on the Master thread. The Master Thread begins processing and then Thread 3 encases its message in an EventArg and invokes the event on the Master Thread. What happens? Does the Master Thread stop in the middle of handling the first event to service the second one? Does the second event wait until the first event is processed? What if Thread 2 gets another message and invokes the event again while the Master is processing the first event?
I wonder if using a queue and an manual Reset EventWaitHandle is not a better idea as the Thread 2 can check the status and wait until it is reset before putting the message in the queue and setting the EventWaitHandle again.
Opinions, please?
Thanks,
Doug
I am a Traveler
of both Time and Space
|
|
|
|
|
I have a similar case: multiple threads accessing temperature controllers over a serial port; a temperature charting thread; another handling user interaction.
When I need to access a shared resource (like the serial port), I’ve been using a C# “lock” at the appropriate point to block a competing thread.
It’s been working well and does not require the threads to do anything “special” (other than insure all requests are short in duration so as not to tie up any one thread for an extended amount of time).
|
|
|
|
|
Good solution! I am using that in a couple of places and that helps. More I am trying to avoid a mix of EventWaitHandles and delegate/event handlers. I think this is a bit "messy" looking. But with schedule tight sometimes you just have to do what you have to do
Doug
I am a Traveler
of both Time and Space
|
|
|
|