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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11625208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:33:05  来源:igfitidea点击:

Accessing UI (Main) Thread safely in WPF

c#.netwpfmultithreadingdatagrid

提问by l46kok

I have an application which updates my datagrid each time a log file that I'm watching gets updated (Appended with new text) in the following manner:

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

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

When the event is raised for the FileWatcher, because it creates a separate thread, when I try to run dataGridRows.Add(ds); to add the new row, the program just crashes without any warning given during debug mode.

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

In Winforms, this was easily solved by utilizing the Invoke function but I am not sure how to go about this in WPF.

在 Winforms 中,这可以通过使用 Invoke 函数轻松解决,但我不确定如何在 WPF 中解决此问题。

采纳答案by Botz3000

You can use

您可以使用

Dispatcher.Invoke(Delegate, object[])

Dispatcher.Invoke(Delegate, object[])

on the Application's (or any UIElement's) dispatcher.

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

You can use it for example like this:

你可以像这样使用它:

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

or

或者

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

回答by Eli Arbel

The best way to go about it would be to get a SynchronizationContextfrom the UI thread and use it. This class abstracts marshalling calls to other threads, and makes testing easier (in contrast to using WPF's Dispatcherdirectly). For example:

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

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

回答by Vineet Choudhary

Use [Dispatcher.Invoke(DispatcherPriority,?Delegate)]to change the UI from another thread or from background.

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

Step 1. Use the following namespaces

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

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

Step 2. Put the following line where you need to update UI

第 2 步。将以下行放在您需要更新 UI 的位置

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

Syntax

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

Parameters

priority

Type: System.Windows.Threading.DispatcherPriority

The priority, relative to the other pending operations in the Dispatcher event queue, the specified method is invoked.

method

Type: System.Delegate

A delegate to a method that takes no arguments, which is pushed onto the Dispatcher event queue.

Return Value

Type: System.Object

The return value from the delegate being invoked or null if the delegate has no return value.

Version Information

Available since .NET Framework 3.0

句法

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

参数

priority

类型: System.Windows.Threading.DispatcherPriority

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

method

类型: System.Delegate

一个不带参数的方法的委托,它被推送到 Dispatcher 事件队列。

返回值

类型: System.Object

被调用的委托的返回值,如果委托没有返回值,则为 null。

版本信息

自 .NET Framework 3.0 起可用