第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在WPF中安全地訪問UI(主)線程

在WPF中安全地訪問UI(主)線程

慕桂英3389331 2019-08-27 10:23:22
在WPF中安全地訪問UI(主)線程我有一個應(yīng)用程序,每次我正在觀看的日志文件更新時都會更新我的數(shù)據(jù)網(wǎng)格(附加新文本),方法如下:private void DGAddRow(string name, FunctionType ft)     {                 ASCIIEncoding ascii = new ASCIIEncoding();     CommDGDataSource ds = new CommDGDataSource();     int position = 0;     string[] data_split = ft.Data.Split(' ');     foreach (AttributeType at in ft.Types)     {         if (at.IsAddress)         {             ds.Source = HexString2Ascii(data_split[position]);             ds.Destination = HexString2Ascii(data_split[position+1]);             break;         }         else         {             position += at.Size;         }     }     ds.Protocol = name;     ds.Number = rowCount;     ds.Data = ft.Data;     ds.Time = ft.Time;     dataGridRows.Add(ds);      rowCount++;     }     ...     private void FileSystemWatcher()     {         FileSystemWatcher watcher = new FileSystemWatcher(Environment.CurrentDirectory);         watcher.Filter = syslogPath;         watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite             | NotifyFilters.FileName | NotifyFilters.DirectoryName;         watcher.Changed += new FileSystemEventHandler(watcher_Changed);         watcher.EnableRaisingEvents = true;     }     private void watcher_Changed(object sender, FileSystemEventArgs e)     {         if (File.Exists(syslogPath))         {             string line = GetLine(syslogPath,currentLine);             foreach (CommRuleParser crp in crpList)             {                 FunctionType ft = new FunctionType();                 if (crp.ParseLine(line, out ft))                 {                     DGAddRow(crp.Protocol, ft);                 }             }             currentLine++;         }         else             MessageBox.Show(UIConstant.COMM_SYSLOG_NON_EXIST_WARNING);     }當(dāng)為FileWatcher引發(fā)事件時,因?yàn)樗鼊?chuàng)建了一個單獨(dú)的線程,當(dāng)我嘗試運(yùn)行dataGridRows.Add(ds)時; 要添加新行,程序只會在調(diào)試模式下沒有任何警告的情況下崩潰。在Winforms中,這可以通過使用Invoke函數(shù)輕松解決,但我不知道如何在WPF中進(jìn)行此操作。
查看完整描述

3 回答

?
aluckdog

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個贊

您可以使用

Dispatcher.Invoke(Delegate, object[])

Application(或任何UIElement)調(diào)度員。

您可以使用它,例如:

Application.Current.Dispatcher.Invoke(new Action(() => { /* Your code here */ }));

要么

someControl.Dispatcher.Invoke(new Action(() => { /* Your code here */ }));


查看完整回答
反對 回復(fù) 2019-08-27
  • 3 回答
  • 0 關(guān)注
  • 1475 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號