I have an event handler, which I want to be handled in the original thread where the object was created, so that it doesn’t block. With forms, it’s easy to use InvokeRequired to force it to the original thread. But how do you do this if your class is not a Form?
Thanks,
PM
This is not going to be easy. First, you will have to create some kind message receiving loop on the thread in question. Then you will need to implement
ISynchronizeInvokein such a manner that it posts a message containing the delegate to execute into a queue that the target thread can pick up and extract that delegate and execute it. The producer-consumer pattern is often useful for setting this up. The important thing to take away is that you cannot just marshal any delegate onto any thread. The target thread has to be specially designed for this to work. It works in UI threads becauseApplication.Rungets a message loop going that theControl.Invokemethod uses.