Take the 2-minute tour ×

I know I can start a new worker thread from with .NET. But how do I start a new UI thread (like in MFC)?

I don't mind if the solution is restricted to Windows boxes only; I also would like the solution to be purely .NET - no p/invokes to CreateThread, etc.

Any input appreciated.

share|improve this question
add comment

4 Answers

up vote 21 down vote accepted

Use Application.Run - it starts a message loop in the current thread. There are overloads to take a form to start with, or an application context, or neither. (If you want to do this for a new thread, you need to create the thread and start it in the normal way, and make it call Application.Run.)

share|improve this answer
How does Application.Run compare with ShowDialog, which is the approach I've been using before? –  supercat Aug 25 '11 at 15:47
@supercat: Are you calling ShowDialog from a non-UI thread then? I'm not sure of the effect in that case. –  Jon Skeet Aug 25 '11 at 16:11
Yeah, I'm calling ShowDialog from a non-UI thread; the apparent effect is that the new window thus created will have a UI thread independent from the UI thread of any other window. It seems to work well provided that the window doesn't try to interact with any controls on other threads, and provided that it doesn't try to set its MdiParent to a window running on another thread. –  supercat Aug 25 '11 at 16:23
@supercat: Have you checked whether it runs a message loop in the calling thread, or just creates a new UI thread and blocks the calling thread until the window has finished? –  Jon Skeet Aug 25 '11 at 16:24
The new window will be responsive even if the main UI thread is blocked, and blocking the new window's UI thread will not affect the main window. I think there may be some issues with things like the clipboard, but mainly I've used this approach for things like progress-update or status windows (e.g. if I have a control display a whirligig in both the main UI window and another window on a different thread, I can visually distinguish between things like garbage-collection which block all threads, and operations which momentarily block the UI thread but let other threads run. –  supercat Aug 25 '11 at 16:30
show 3 more comments

If you are interested in using WPF, check out MSDN's article WPF's Threading Model, in particular the "Multiple Windows, Multiple Threads" section. It details what you need to do to create a new Dispatcher and show a window on a new thread.

share|improve this answer
Great article, thanks :) –  Tom Feb 22 '11 at 23:22
add comment

If you're looking to create a new thread that is capable of creating and dealing with Window handles, then you can use the SetApartmentState function on the Thread object and set it to STA. You'll also likely need to call Application.Run inside the method for this thread in order to create your message loop. However, bear in mind that you're subject to the same cross-threading no-no's that you have in any other thread (ie, you can only interact with the handle for a control on the thread inside which it was created), so you can't do anything on your other UI thread without switching contexts.

share|improve this answer
add comment

Also you can do this:

    delegate void MyProcHandler(object param1, object param2);

            new MyProcHandler(delegate(object param1, object param2) 
            // some code
share|improve this answer
add comment

Your Answer


