本文介绍了WPF中的Thread.Join UI块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义MessageWindow以便摆脱旧的样式,并在上面放上自己的样式...

我的问题是,例如,当我单击一个按钮打开自定义MessageWindow时,它并没有真正阻塞我的UI.

public static void Show(string caption, string message)
{
    Thread thread = new Thread(() =>
    {
        MessageWindow window = new MessageWindow(caption, message);
        window.ShowDialog();
    });
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}

我认为thread.Join会阻塞父线程,直到新线程消失,但不知何故UI处于部分活动状态.我看不到动画,也无法移动窗口,但是当我在MessageWindow打开时单击被阻止线程上的按钮时,它仍然接受单击并在关闭MessageWindow线程后执行它. /p>

打开消息窗口时,是否有任何方法禁用消息泵或阻止/锁定任何线程/UI?

解决方案

您不应为其他窗口创建新线程.使用WPF时,所有UI都应在同一线程上运行.

当您要执行与UI无关并且不想阻止UI的操作时,请创建新线程.

I created a custom MessageWindow in order to get rid of the old one and put my own style on it...

My problem is when I for example click a button to open the custom MessageWindow it doesn't really block my UI.

public static void Show(string caption, string message)
{
    Thread thread = new Thread(() =>
    {
        MessageWindow window = new MessageWindow(caption, message);
        window.ShowDialog();
    });
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}

I thought thread.Join will block the parent thread until the new one is gone, but somehow the UI is partially active. I don't see animations nor I'm able to move the window but when I click a button on the blocked thread while my MessageWindow is open it still accept the click and perform it after I closed the MessageWindow thread.

Is there any way to disable the message pump or block/lock any threads/UI when the message window is open?

解决方案

You shouldn't create a new thread for the other window. When using WPF, all UI should run on the same thread.

Create new threads when you want to do something that is unrelated to UI and don't want to block the UI.

这篇关于WPF中的Thread.Join UI块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:03