Saschanski
Saschanski

Reputation: 153

Thread.Join UI block in WPF

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?

Upvotes: 1

Views: 814

Answers (2)

Mike Strobel
Mike Strobel

Reputation: 25623

If you want to prevent the user from interacting with the main window while a MessageWindow is visible, simply set the Owner of the MessageWindow to your application's main window. Then call ShowDialog() from the UI thread, and the behavior should be comparable to if you called MessageBox.Show().

Do not use a separate thread or attempt to manually block the message pump.

Upvotes: 0

Venemo
Venemo

Reputation: 19097

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.

Upvotes: 1

Related Questions