开发者

Can I run a method on the main thread from a separate thread?

开发者 https://www.devze.com 2023-02-02 01:45 出处:网络
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 st

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消