Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, August 24, 2009 8:13 AM
I ve a MDI form, under which i ve 3 Diff childform.
All 3 child forms having a seperate datagridview which is being updated on every second.
I want to use BackgroundWorker or Thread to open and process all child forms under parent form,
because of MDI parent form, I want to open all forms using Form.show() method only, so user can see all the data on a single scrren using cascading style.
How, can i open all child on seperate thread, which remains live and process data and update datagrid view automatically, by kepping main thread free for communication purpose only.
Point to be taken care
- I cant use showdialog method.
- all form must be processed continuously by seperate threads
- each form must update their own datagridview efficiently.
Any help will be appreciated.
Thanks,
Thanks for the help!
All replies (2)
Monday, August 24, 2009 5:08 PM âś…Answered
It is okay to create a new form on a properly configured thread. But that form cannot be an MDI child form, Really Bad Things happen when that child sends a message to its parent. Just for completeness, the code to create a threaded form:
private void button1_Click(object sender, EventArgs e) {
Thread t = new Thread(threadedForm);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void threadedForm(object arg) {
Application.Run(new Form2());
}
Hans Passant.
Monday, August 24, 2009 8:51 AM | 1 vote
I don't think you will be able to actually 'open the forms on another thread' because they are children of the parent form and Windows Forms is very fussy about parents and children being on the same thread.
Likely you will have to create a background thread that retrieves the data and then passes it on, using invoke and a delegate, to the appropriate form. Updating the user interface MUST be done on the thread the form was created on and since that needs to be the same as the parent all you can do in the background thread is retrieve/modify the data before displaying it.
Given you can't show the child windows on a second thread, you can show them using .Show() normally, you just need to implement a thread and logic elsewhere to manage the data and pass it off the forms at the appropriate times.
You could have a seperate thread that manages/retrieves the data for each form, but again all the forms will need to exist and be updated on the one UI thread.