Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用来自后台线程的信息更新WPF窗口?_C#_Wpf_Multithreading_User Interface - Fatal编程技术网

C# 如何使用来自后台线程的信息更新WPF窗口?

C# 如何使用来自后台线程的信息更新WPF窗口?,c#,wpf,multithreading,user-interface,C#,Wpf,Multithreading,User Interface,我正在创建一个wpf应用程序,它在后台执行许多任务,但仍然需要UI做出响应并显示各种后台任务的状态。它还可以选择根本不显示UI,在这种情况下,应该放弃状态消息,而不创建主窗体的实例 我尝试的是删除 StartupUri=“MainWindow.xaml” 来自App.xaml。然后,在App.xaml.cs中,我有 ` 这将显示主窗体(如果需要),并启动所有后台进程。这部分是有效的 为了从后台向UI发送消息,我实现了一个非常基本的messenger: ` 为什么我的后台进程冻结了我的UI?我能做

我正在创建一个wpf应用程序,它在后台执行许多任务,但仍然需要UI做出响应并显示各种后台任务的状态。它还可以选择根本不显示UI,在这种情况下,应该放弃状态消息,而不创建主窗体的实例

我尝试的是删除

StartupUri=“MainWindow.xaml”

来自App.xaml。然后,在App.xaml.cs中,我有

`

这将显示主窗体(如果需要),并启动所有后台进程。这部分是有效的

为了从后台向UI发送消息,我实现了一个非常基本的messenger:

`


为什么我的后台进程冻结了我的UI?我能做些什么来防止它?为什么我的UI没有更新状态消息?

也许这就是你的问题:

   Dispatcher.Invoke(DispatcherPriority.Normal,
                new Action<string>(AddText),
                message);
Dispatcher.Invoke(DispatcherPriority.Normal,
新行动(AddText),
信息);
尝试将此更改为

   Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new Action<string>(AddText),
                message);
Dispatcher.BeginInvoke(DispatcherPriority.Normal,
新行动(AddText),
信息);
因为当您使用
Invoke
时,该方法会被执行,应用程序会等待它完成,但是使用
BeginInvoke
时,该方法会被异步调用,并且在执行
BeginInvoke
中引用的方法时,应用程序会继续执行


阅读以下内容:

使用以下代码避免冻结UI。在我的应用程序中,我使用了BackgroundWorker类。通过使用代码更改表单上的任何内容,将引发运行时错误

我使用下面的代码来避免这种情况,它非常适合我

Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
{
   rtf_status.AppendText("Validating XML against schema...Please wait\n");
}));

请注意大括号({})后面的部分,如果希望更改表单上的某些内容,则应将代码放在此处。

考虑使用模式作为MVVM,将视图绑定到模型数据。我遇到了同样的问题,并帮助我解决了它。似乎我需要做的是显式地将我的后台线程作为新线程启动。我假设,只要在我的App_启动方法的末尾调用它们,它们就位于到UI的单独线程上。情况似乎并非如此。当我在一个新线程中启动该流程时,所有其他部分都完全按照预期工作。谢谢你的链接。谢谢,很高兴它解决了你的问题。从本质上说,这段代码和我的代码没有什么不同,你只是使用了一个匿名方法,而我使用的是一个命名方法。在Invoke和BeginInvoke之间交换不会在我的应用程序中产生明显的差异。
   Dispatcher.Invoke(DispatcherPriority.Normal,
                new Action<string>(AddText),
                message);
   Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                new Action<string>(AddText),
                message);
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
{
   rtf_status.AppendText("Validating XML against schema...Please wait\n");
}));