开发者

wpf method to start the application message loop

I need to create a Form in a different thread and keep it ranning until a user performe some action in the main thread (click a button).

It is not very difficult to do using

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

that starts an ap开发者_开发知识库plication message loop in the current thread. But this solution requires the usage of System.Windows.Forms namespace, which in not a wpf namespace.

Do you know a wpf-way of achieving this goal? =)

P.S. without starting an application message loop the thread will be imidiatly terminated after processing of the last expression in it. So, the Form will appear only for a moment and will be closed. =(


Use System.Windows.Threading.Dispatcher.Run()


And, there's also Dispatcher.PushFrame

This is handy because it allows you to run the messageloop until a stop criterion you define, for instance while a progress dialog is on the screen.


Does this help ?

Application.DoEvents in WPF Revisited

Code extract 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 { }));
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜