C# 在WPF中安全访问UI(主)线程
我有一个应用程序,每当我正在观看的日志文件以以下方式更新(附加新文本)时,它都会更新我的datagrid:C# 在WPF中安全访问UI(主)线程,c#,.net,wpf,multithreading,datagrid,C#,.net,Wpf,Multithreading,Datagrid,我有一个应用程序,每当我正在观看的日志文件以以下方式更新(附加新文本)时,它都会更新我的datagrid: private void DGAddRow(string name, FunctionType ft) { ASCIIEncoding ascii = new ASCIIEncoding(); CommDGDataSource ds = new CommDGDataSource(); int position = 0; s
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);
}
当我尝试运行dataGridRows.Add(ds)时,为FileWatcher引发事件,因为它创建了一个单独的线程;要添加新行,程序只是在调试模式下崩溃而没有给出任何警告
在Winforms中,通过使用Invoke函数可以很容易地解决这个问题,但我不确定如何在WPF中实现这一点 您可以使用
在应用程序
的调度程序(或任何ui元素
的调度程序)上
例如,您可以这样使用它:
Application.Current.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);
}
}
用于从其他线程或后台更改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
)
参数
优先级
类型:System.Windows.Threading.DispatcherPriority
相对于中的其他挂起操作的优先级
在调度程序事件队列中,将调用指定的方法
方法
类型:System.Delegate
对不带参数的方法的委托,该方法被推送到
调度程序事件队列
返回值
类型:System.Object
正在调用的委托的返回值,如果
委托没有返回值
版本信息
从.NET Framework 3.0开始提供
上述方法出现错误,因为运行该行时Application.Current为null。为什么会出现这种情况?您可以使用任何UIElement来实现这一点,因为每个UIElement都有“Dispatcher”属性。@l46kok这可能有不同的原因(控制台应用程序、从winforms托管等)。正如@WolfgangZiegler所说的,您可以使用任何UIElement。我通常只使用
Application.Current
,因为我觉得它看起来更干净。@Botz3000我想这里也发生了一些竞争条件问题。在附加上面给出的代码之后,当我进入调试模式并手动执行分步时,代码可以完美地工作,但是当我在没有调试的情况下运行应用程序时,代码崩溃。我不确定在这里锁定什么导致了问题。@l46kok如果您认为这是一个死锁,您也可以调用Dispatcher.BeginInvoke
。该方法只是将委托排队等待执行。非常感谢!接受的解决方案在每次调用时都开始挂起,但这是可行的。当从包含视图模型但没有“真实”WPF(即类库)的程序集调用时,它也可以工作。这是一个非常有用的提示,特别是当您有一个非WPF组件,其中包含一个要封送操作的线程时。当然,另一种方法是使用TPL continuation。起初我不理解,但它对我很有用。。不错。(应该指出DGAddRow是一个私有方法)您应该提到\u syncContext.Post(o=>
是一个异步的“激发和遗忘”。要进行同步调用,应该使用\u syncContext.Send(o=>
)。
[BrowsableAttribute(false)]
public object Invoke(
DispatcherPriority priority,
Delegate method
)