Raising an event between two threads when no thread is WinForm

I find it difficult to find a C # example that shows how to raise a cross-stream event in the following state:

Let's say I have 1 event and 3 topics:

DoStuff Event

Thread A - WinForm

Thread B - Thread created from Thread A to do some processing. Has a Foo () function that is subscribed to DoStuff

Thread C - Thread spawned by thread B to execute some subprocess and Raises DoStuff event

Now How to ensure that an event raised in thread C is handled inside Thread B instead of C or A.

All the samples I'm scrolling point to Form / Control.Invloke or something like that, where I really want any thread actually subscribed to the event to execute a repressive thread inside it, and not just the main thread form.

+2
source share
1 answer

Marching a call from one thread to a specific other thread is very nontrivial. It is not possible to arbitrarily interrupt a stream and force it to execute some code. This causes terrible reconnection issues. Trying to protect against this with, say, a semaphore is guaranteed to cause a dead end.

The target thread must interact, it must be "inactive" and not actively change the state of the program. A common mechanism for this is a thread-safe queue. The thread for creating events queues the request; the target thread needs a loop that reads the requests from the queue and executes them. Maybe this sounds familiar, yes, that the user interface thread of the program works.

+4
source

All Articles