3

I've an application where I need to open many forms which are heterogeneous to my own and run independently. My application then proceeds to block on long running operations (making those operations asynchronous is not possible). So I would like to run these forms on a separate thread with their own message pump.

Any way to do that?

pnuts
  • 58,317
  • 11
  • 87
  • 139
esac
  • 24,099
  • 38
  • 122
  • 179

2 Answers2

3
Application.Run();

This should launch the form on its own thread, with its own message pump.

EDIT: http://en.csharp-online.net/Application_Architecture_in_Windows_Forms_2.0%E2%80%94Application_Lifetime

Since a new UI thread is created whenever Application.Run is invoked, this should accomplish what you are looking for.

EDIT #2: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.run(VS.71).aspx

The documentation is a little ambiguous. I've always accomplished long running operations by using a ThreadPool and periodically marshalling control back to the form to render status, but it sounds like you could spawn multiple forms with Application.Run().

Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • 1
    The answer could be more precise - Create a separate thread. In it's worker function, use `Application.Run(Form)`. I hope I interpreted it right. :) – Nayan Oct 07 '10 at 05:40
  • @Nayan - +1 That's how I read it too, but I can't speak precisely on this one so I would rather link to the documentation than provide the WRONG answer. – Tim M. Oct 07 '10 at 05:49
  • Yeah... documentation.. tell me about it. Sigh! – Nayan Oct 07 '10 at 06:01
0

Use the overload Application.Run(ApplicationContext). Create an ApplicationContext before, give it the form (context.MainForm=yourForm). When form closed or otherwise done, call ApplicationContext.ExitThread(). Happy coding.

J S
  • 901
  • 8
  • 15