Run event handlers in another thread (without threads blocking)

Kornelije Petak picture Kornelije Petak · Jul 7, 2011 · Viewed 11k times · Source

I have a class Communicator that works in a background thread receiving data on a TCP 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?

Answer

David Anderson picture David Anderson · Jul 7, 2011

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.