2 回答

TA貢獻1820條經驗 獲得超9個贊
您遺漏了一些細節(jié),主要是事件處理程序在 GUI 線程上執(zhí)行的操作。Invoke 或 BeginInvoke 等。
但如果保護您的數據最重要,還有另一種選擇:將新數據推送到 ConcurrentQueue。引發(fā) Received 事件是可以的,但可選,您可能不需要它。
主線程可以在自己的時間清空隊列。例如使用定時器。
您的屏幕更新仍然會斷斷續(xù)續(xù),但您不應再丟失數據。

TA貢獻2011條經驗 獲得超2個贊
您必須拆分(線程方式)兩件事:后臺工作和繪圖工作。一般來說,這是如何做這些事情的方式,但要具體 - 如果您的繪圖需要時間,那么您的工作線程可能無法按時處理傳入的數據,您可能會丟失一些數據/ 省略(這就是您實際觀察到的)。
這是一種方法(該方法必須是 UI 類的成員 - 窗口、用戶控件等):
void OnDataReceived(object sender, DataEventArgs e)
{
// here we're in the context of the working thread
// this call will return immediately giving control back to the working thread
Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
(Action)delegate
{
// here we are in the context of the UI thread
});
}
- 2 回答
- 0 關注
- 152 瀏覽
添加回答
舉報