You can use Form.Load - it fires after initialization but before the Form is displayed (it's safe to access the Form child controls in the Load event). Given you're going to fire off a background thread, this will effectively work as if you started the work when the Form is displayed.
Note that you cannot fill the DataGrid on the background thread as Windows Forms doesn't support control access from the non-UI thread. You can fill a DataSet (or equivalent) on the background thread and then bind it to the DataGrid on the UI thread. If you are using VS 2005, you can use the BackgroundWorker component to simplify this task.
Also note that if for some reason, you really need to do your work later than the Form.Load, then you can do the equivalent of PostMessage by calling this.BeginInvoke on a method (this calls PostMessage). For example, you could call this.BeginInvoke in your Form.Load to post a message to the Form. The VS 2005 code for this would look like this:
| | private void LoadData() { /* Fire off background worker here */ } private void Form2_Load(object sender, EventArgs e) { this.BeginInvoke(new MethodInvoker(this.LoadData)); } |
Joe Stegman
The Windows Forms Team
Microsoft Corp.
This posting is provided "AS IS" with no warranties, and confers no rights.