0

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().

Lahiru
  • 1,428
  • 13
  • 17
Vladimir Kruglov
  • 196
  • 1
  • 19

1 Answers1

0

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.

Lahiru
  • 1,428
  • 13
  • 17
Vladimir Kruglov
  • 196
  • 1
  • 19
  • 1
    So you think it works but you don't know how or why? Feel confident to go forward? – H H Jan 04 '14 at 12:29