Dispatcher.Invoke挂起主窗口 [英] Dispatcher.Invoke hangs main window

查看:133
本文介绍了Dispatcher.Invoke挂起主窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在主窗口中创建了新的WPF项目:

I've created new WPF project, in main window I'm doing:

public MainWindow()
{
    InitializeComponent();

    Thread Worker = new Thread(delegate(){

        this.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, new Action(delegate
        {
            while (true)
            {
                System.Windows.MessageBox.Show("asd");

                Thread.Sleep(5000);
            }
        }));
    });

    Worker.Start();
}

问题在于这些消息在MainWindow挂起之间。我如何使其异步工作?

the problem is between those messages MainWindow hangs. How do I get it work asynchronously?

推荐答案

因为您告诉UI线程处于休眠状态,并且您没有让调度程序返回

Because you are telling the UI thread to sleep and you are not letting the dispatcher return to processing its main message loop.

尝试类似的东西

Thread CurrentLogWorker = new Thread(delegate(){
   while (true) {
      this.Dispatcher.Invoke(
                 DispatcherPriority.SystemIdle, 
                 new Action(()=>System.Windows.MessageBox.Show("asd")));
      Thread.Sleep(5000);
   }
});    

这篇关于Dispatcher.Invoke挂起主窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆