How to start UI thread in C #

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

I do not mind if the solution is limited only to Windows boxes; I would also like the solution to be purely .NET - there are no p / invokes for CreateThread, etc.

Any input is appreciated.

+8
multithreading user-interface c #
Apr 13 '09 at 19:24
source share
4 answers

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

+24
Apr 13 '09 at 19:26
source share

If you are interested in using WPF, see the MSDN article Threading Model WPF , in particular the section “Multiple Windows, Multiple Threads”. It details what you need to do to create a new dispatcher and show the window in a new thread.

+4
Apr 13 '09 at 19:38
source share

If you want to create a new thread that can create and process Window handles, you can use the SetApartmentState function of the SetApartmentState object and set it to STA . You will also probably need to call Application.Run inside the method for this thread to create your message loop. However, keep in mind that you undergo the same cross-stream no-no that you have in any other stream (i.e. you can only interact with the handle to the control in the stream inside which it was created), therefore you cannot do anything in your other user interface thread without switching contexts.

+2
Apr 13 '09 at 19:31
source share

You can also do this:

  delegate void MyProcHandler(object param1, object param2); MyForm.Invoke ( new MyProcHandler(delegate(object param1, object param2) { // some code }), null, null ); 
0
Apr 13 '09 at 19:40
source share



All Articles