在WPF中安全访问UI(主)线程



我有一个应用程序,每当我正在观看的日志文件以以下方式更新(添加新文本)时,它都会更新我的数据网格:

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);
    }

当为FileWatcher引发事件时,因为它创建了一个单独的线程,当我尝试运行dataGridRows.Add(ds);为了添加新行,程序在调试模式下崩溃,没有任何警告。

在Winforms中,通过使用Invoke函数可以很容易地解决这一问题,但我不确定如何在WPF中实现这一点。

您可以使用

Dispatcher.Invoke(Delegate, object[])

Application的(或任何UIElement的)调度器上。

你可以这样使用它:

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

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

最好的方法是从UI线程获取SynchronizationContext并使用它。这个类抽象了对其他线程的编组调用,并使测试更容易(与直接使用WPF的Dispatcher相比)。例如:

class MyViewModel
{
    private readonly SynchronizationContext _syncContext;
    public MyViewModel()
    {
        // we assume this ctor is called from the UI thread!
        _syncContext = SynchronizationContext.Current;
    }
    // ...
    private void watcher_Changed(object sender, FileSystemEventArgs e)
    {
         _syncContext.Post(o => DGAddRow(crp.Protocol, ft), null);
    }
}

请注意,通常不建议使用Send方法——我们会不必要地阻止线程池线程等待UI更新,而不是采用异步方法,即使用事件(如属性和集合更新)等待UI更新并释放该线程来做其他工作。

使用[Dispatcher.Invoke(DispatcherPriority,Delegate)]从另一个线程或从后台更改UI。

步骤1。使用以下命名空间

using System.Windows;
using System.Threading;
using System.Windows.Threading;

步骤2。在需要更新UI 的地方放上以下行

Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate
{
    //Update UI here
}));

语法

[BrowsableAttribute(false)]
public object Invoke(
  DispatcherPriority priority,
  Delegate method
)

参数

priority

型号:System.Windows.Threading.DispatcherPriority

相对于中的其他挂起操作的优先级Dispatcher事件队列,将调用指定的方法。

method

型号:System.Delegate

对不带参数的方法的委托,该方法被推到Dispatcher事件队列。

返回值

型号:System.Object

正在调用的委托的返回值,或者如果委托没有返回值。

版本信息

自.NET Framework 3.0 起提供

最新更新