Run event handlers in another thread (without threads blocking)
I have a class Communicator
that works in a background thread receiving data on a T开发者_StackOverflowCP port.
The Communicator has an event OnDataReceived
which is of a EventHandler<DataReceivedEventArgs>
type.
There is another class Consumer
that contains a method subscribed to the Communicator.OnDataReceived
event.
comm.OnDataReceived += consumer.PresentData;
The Consumer
class is created within a Form constructor and then one of its methods is called on another thread. This method is an infinite loop, so it stays in that method during the application execution.
What I'd like to do is for the Communicator.OnDataReceived
event to invoke the consumer.PresentData
method on consumer's thread.
Is that even nearly possible? And if it is, what kind of mechanisms (sync classes) should I use?
Add this somewhere in your code: (I usually put this in a static helper class called ISynchronizedInvoke so I can call ISynchronizedInvoke.Invoke(...));
public static void Invoke(ISynchronizeInvoke sync, Action action) {
if (!sync.InvokeRequired) {
action();
}
else {
object[] args = new object[] { };
sync.Invoke(action, args);
}
}
Then inside OnDataReceived, you could do:
Invoke(consumer, () => consumer.PresentData());
This invokes 'consumer.PresentData' on 'consumer'.
As for your design issue (consumer references communicator), you could introduce a method inside communicator such as:
class Communicator {
private ISynchronizeInvoke sync;
private Action syncAction;
public void SetSync(ISynchronizeInvoke sync, Action action) {
this.sync = sync;
this.syncAction = action;
}
protected virtual void OnDataReceived(...) {
if (!sync.InvokeRequired) {
syncAction();
}
else {
object[] args = new object[] { };
sync.Invoke(action, args);
}
}
}
This would give you a way to pass in the ISynchronizedInvoke from your consumer class. So you would be creating the ISynchronizedInvoke in the consumer assembly.
class Consumer {
public void Foo() {
communicator.SetSync(this, () => this.PresentData());
}
}
So basically you are creating everything you need to do the invoke, and just passing it in to your communicator. This resolves your necessity to have an instance or reference to consumer in communicator.
Also note that I did not test any of this I am doing this all in theory, but it should work nicely.
Try to use the BackgroundWorker class.
It should be possible. You may create a queue for execution, or look at the Dispatcher object, it's useful (and sometimes mandatory as the only way) to push some methods into the UI Thread, it that helps.
You can get a method to execute on a thread only if the target thread is designed to accept a marshaling operation that transfers the execution of the method from the initiating thread to the target thread.
One way to get this to work is to have your Consumer
class implement ISynchronizeInvoke
. Then have your Communicator
class accept an ISynchronizeInvoke
instance that it can use to perform the marshaling operation. Take a look at the System.Timers.Timer
class as an example. System.Timers.Timer
has the SynchronizingObject
property that it can use to marshal the Elapsed
event onto the thread hosting the synchronizing object by calling ISynchronizeInvoke.Invoke
or ISynchronizeInvoke.BeginInvoke
.
The tricky part is how you implement ISynchronizeInvoke
on the Consumer
class. The worker thread started by that class will have to implement the producer-consumer pattern to be able to process delegates. The BlockingCollection
class would make this relatively easy, but there is still quite a learning curve. Give it a shot and post back with a more focused question if you need more help.
精彩评论