How to open a window in a separate thread?

前端 未结 4 1797
慢半拍i
慢半拍i 2021-01-19 13:35

I\'d like to do:

Window newWindow = new Window();
newWindow.Show();

while (true) 
{
    Console.Write(\"spin\");
}

I.e., I\'m doing an int

4条回答
  •  终归单人心
    2021-01-19 14:22

    You shouldn't try to open a new Window in it's own thread.

    Instead, push the computational work you're doing into a background thread, and leave the UI thread free. The simplest option for this is typically to use BackgroundWorker. It handles the marshaling of progress and completion back to the UI thread for you automatically.

    However, you can do this yourself using threads or Task/Task, as well. Just remember to use the Dispatcher or a TaskScheduler created from the UI context for anything that will update the UI, such as progress notifcication.

提交回复
热议问题