问题
Is it possible to run each child form of a MDI form on a separate thread? If so, can you please give some codes and example how to setup this?
Thanks!
回答1:
I can't tell from the question wether you are aware of the exotic nature of the solution you are proposing. I apologize if this answer completely misses the mark.
I assume that you want responsiveness in your application. That you don't want other forms to "hang" while one form is doing work.
However, having a different GUI thread per form is not how one usually achieves this. I don't know if it is even possible.
You will still have only one thread handling all the graphics (aka "the GUI Thread"), but all time consuming work should be immediately offloaded to another thread (aka worker thread). That way the app remains responsive.
I suggest you check out this video. Applicable or not, within 4-6 minutes, you should know wether this is the answer you are looking for.
DNRTV episode with Stephen Toub
Another possibility is that you are asking about how to display a form without it being modal.
回答2:
No.
Any GUI activity has to happen on the main thread.
Processing can be done in a separate thread, in which case, try using the BackgroundWorker
in your child forms.
回答3:
This MSDN forum post says that forms can be run in separate threads. I don't know if it's applicable to child MDI forms, though. But the technique is simple enough:
Thread thread = new Thread( () =>
{
var yourForm = new YourForm();
Application.Run(yourForm);
});
thread.ApartmentState = ApartmentState.STA;
thread.Start();
来源:https://stackoverflow.com/questions/7915795/running-each-child-form-as-a-separate-thread-in-a-mdi-container