Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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/6/ant/2.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#_.net_Wpf_Multithreading - Fatal编程技术网

C# 用于启动应用程序消息循环的wpf方法

C# 用于启动应用程序消息循环的wpf方法,c#,.net,wpf,multithreading,C#,.net,Wpf,Multithreading,我需要在不同的线程中创建一个表单,并保持它的运行状态,直到用户在主线程中执行某些操作(单击按钮) 这不是很难做到使用 System.Windows.Forms.Application.Run(new ApplicationContext()); 在当前线程中启动应用程序消息循环。 但此解决方案需要使用System.Windows.Forms命名空间,它不在wpf命名空间中 您知道wpf实现此目标的方法吗?=) 另外,如果不启动应用程序消息循环,线程将在处理其中的最后一个表达式后立即终止。因此,

我需要在不同的线程中创建一个表单,并保持它的运行状态,直到用户在主线程中执行某些操作(单击按钮)

这不是很难做到使用

System.Windows.Forms.Application.Run(new ApplicationContext());
在当前线程中启动应用程序消息循环。 但此解决方案需要使用System.Windows.Forms命名空间,它不在wpf命名空间中

您知道wpf实现此目标的方法吗?=)


另外,如果不启动应用程序消息循环,线程将在处理其中的最后一个表达式后立即终止。因此,表单将只显示一小会儿,并将被关闭=(

使用System.Windows.Threading.Dispatcher.Run()

这有帮助吗

从博客中提取代码:

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

namespace Sheva.Windows
{
    /// <summary>
    /// Designates a Windows Presentation Foundation application with added functionalities.
    /// </summary>
    public class WpfApplication : Application
    {
        /// <summary>
        /// Processes all messages currently in the message queue.
        /// </summary>
        /// <remarks>
        /// This method can potentially cause code re-entrancy problem, so use it with great care.
        /// </remarks>
        public static void DoEvents()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
        }
    }
}
使用系统;
使用系统线程;
使用System.Windows;
使用System.Windows.Threading;
名称空间Sheva.Windows
{
/// 
//指定具有附加功能的Windows演示基础应用程序。
/// 
公共类WPF应用程序:应用程序
{
/// 
///处理消息队列中当前的所有消息。
/// 
/// 
///此方法可能会导致代码重入问题,因此请谨慎使用。
/// 
公共静态void DoEvents()
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,newthreadstart(委托{}));
}
}
}

还有
Dispatcher.PushFrame


这很方便,因为它允许您运行messageloop,直到您定义停止条件为止,例如,当进度对话框出现在屏幕上时。

非常有趣的方法。您有使用它的示例吗?我手头没有任何东西,我在以前的公司使用过它。我想我通常创建DispatcherFrame,然后创建对话框窗口中,为其提供对DispatcherFrame对象的引用,然后调用PushFrame。当处理完成或用户取消时,您将DispatcherFrame的Continue属性设置为false。在线程化代码中,我首先向后台线程发出取消的信号,然后等待它向UI线程发出确实取消的信号,然后继续=该事件为false。
Dispatcher.Run
只调用
Dispatcher.PushFrame(new DispatcherFrame());
。因此它是相同的。