4

I'd like to do:

Window newWindow = new Window(); newWindow.Show(); while (true) { Console.Write("spin"); } 

I.e., I'm doing an intensive calculation in main window, but this new window (where I'm trying to show a busy indicator with an animation) is not responding (it's frozen)...

So I tried to do:

Thread thread = new Thread(() => { Window newWindow = new Window(); newWindow.Show(); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); while (true) { Console.Write("spin"); } 

But the new window is still frozen, etc. Anyone know what's wrong here?

1
  • 1
    Why not just perform your work on another thread? Commented Apr 6, 2011 at 17:02

4 Answers 4

8

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<T>, 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.

Sign up to request clarification or add additional context in comments.

5 Comments

the "computation work" is actually a new window that's being displayed (in addition to newWindow that is). I'm loading a new window which takes a while to load.
@foreyez: You can typically have the "new window" move the non-UI computations into a background thread, and avoid this problem entirely.
I think he is recommending put the work done in the New or Load method of the window in a new thread. The window can then open and show a loading message (if in the load event)
@foreyez: It sounds like you're saying that the slow computational work is currently tightly coupled into the window creation routine. Same advice applies: split the long work out into its own thread that sends asynchronous messages to the window.
You'll want to extract the work that causes the new window to take a while into the background thread, either done before the load or with a progress indicator as it's running. Loading a window and placing controls only shouldn't take a long time.
2

You don't - there's only one GUI thread. If you need to do blocking work, that stuff has to go in a background thread.

Comments

2

You need to run a separate message loop in the new thread using the Application class.

1 Comment

@Henk: Yes; I realize that it's bad advice. However, the other answers already covered that.
1

@SLaks This can be very useful if you are trying to launch a new window out of a console app. One option is to use showDialog(); But then its difficult to close it.

If you use Show() and then an Application.run() (this is the nuts and bolts of using the Application class to start a new message loop)

When you call Application.Exit() in your main console thread, then the app actually closes;

using showDialog(), you have to abort the thread, and then wait until the window gets some kind of input ie mouse over or focus. else it will hange on exit forever.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.