Вопрос

I created a new window in a separate dispatcher

Dispatcher dispatcher = null;
var newWindowThread = new Thread(() =>
{
    MainWindow window = new MainWindow();
    window.ShowDialog();
});

newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = true;
newWindowThread.Start();
while (dispatcher == null)
{
    Thread.Sleep(10);
    dispatcher = Dispatcher.FromThread(newWindowThread);
}

and then using this dispatcher I can Invoke some actions, but in this case this window is modal, I don't like it and I need a separate window which is not modal.

When I use Show() instead of ShowDialog() then execution goes to the end and my dispatcher will not work.
How can I put it in a infinite loop?
But tread needs to be active and response to Invoke().

Это было полезно?

Решение

I have found a solution:

var newWindowThread = new Thread(() =>
{
    MainWindow window = new MainWindow();
    window.Show();
    window.Closed += window_Closed; //here we have method which will shutdown current Dispatcher when window is closed
    Dispatcher.Run();
});

Still don't know exactly how Dispatcher.Run(); works, but it does what I need.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top