C# 在Windows 7中从其他线程更新进度条
我正在尝试更新一个包含标签和进度条的对话框。实际上,在我的应用程序中,我正在备份数据,在备份时,我希望显示进度条和一个标签,显示当前正在备份的内容。我使用这个函数调用进度C# 在Windows 7中从其他线程更新进度条,c#,wpf,progress-bar,dispatcher,C#,Wpf,Progress Bar,Dispatcher,我正在尝试更新一个包含标签和进度条的对话框。实际上,在我的应用程序中,我正在备份数据,在备份时,我希望显示进度条和一个标签,显示当前正在备份的内容。我使用这个函数调用进度 Application.Current.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, (Action)(() => { dialog.ProgressbarValue = "Backing up first item"; })); 这在Windows8上运
Application.Current.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, (Action)(() => { dialog.ProgressbarValue = "Backing up first item"; }));
这在Windows8上运行良好,但在Windows7上没有任何更新 您需要在文本框上应用刷新,以便在循环运行时显示所有文本 您需要有一个包含刷新例程的ExtensionMethods静态类
public static class ExtensionMethods
{
private static Action EmptyDelegate = delegate() { };
public static void Refresh(this UIElement uiElement)
{
uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
}
}
每次设置dialog.ProgressbarValue后,从代码中调用此ExtensionMethod
注意:您不需要在这个逻辑简单设置dialog.ProgressbarValue的代码中设置Dispatcher,并使用dialog.refresh()调用上面的refresh方法 设置
DispatcherPriority.ApplicationIdle
实际上解决了这个问题您是否尝试过任何其他DispatcherPriority,可能是后台
?是的,DispatcherPriority.Send
即使在Windows 8上也不起作用DispatcherPriority.Loaded
在Windows 8上有效,但在Windows 7上无效为什么不使用普通优先级?无论操作系统版本如何,这都应该是一样的。@AdrianFaciu Normal priority现在正在更新任何内容。