简体   繁体   中英

How to run a form with its own message pump?

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?

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().

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM