Wpf method to start application message loop

I need to create a form in another thread and save it until the user performs some action in the main thread (click the button).

This is not very difficult to do using

System.Windows.Forms.Application.Run(new ApplicationContext());

which starts the application message loop in the current thread. But this solution requires the use of the System.Windows.Forms namespace, which is not a wpf namespace.

Do you know the wpf way to achieve this? =)

PS without starting the application message loop, the thread will be finely terminated after processing the last expression in it. So, the form will appear only for a moment and will be closed. = (

+5
source share
3

System.Windows.Threading.Dispatcher.Run()

+4

Dispatcher.PushFrame

, messageloop , , , .

+4

Does it help?

Application.DoEvents in WPF Revisited

Output code from the blog:

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 { }));
        }
    }
}
-1
source

All Articles