Can I run a method on the main thread from a separate thread?
I'm reading data from a serial port, but the DataReceived event of SerialPort is handled on it's own thread. I want to handle this on the main thread, but simply declaring an event and raising it still results in it being processed on the SerialPort thread. I'm assum开发者_运维技巧ing I need to declare a delegate I can call, but I don't see how that would work.
For example, I want to call Sub HandleDataReceived() on the main thread from the DataReceived thread, having HandleDataReceived() run on the main thread. How would I do this?
If the main thread is a UI thread, you can use its SynchronizationContext
or call Control.BeginInvoke
.
精彩评论