When you use
this.Invoke
to call
GetData
, you are forcing
GetData
to execute on the UI thread. This means that any interaction with the main window or the dialog window will have to wait for
GetData
to finish executing.
Try changing your button click event to:
Thread t = new Thread(new ThreadStart(delegate
{
this.Invoke(new Action<bool>(StartLoading), true);
GetData();
this.Invoke(new Action<bool>(StartLoading), false);
}));
t.Start();
ShowDialog
is a blocking call, which means it will show the window and wait for the user to close the window before continuing with the next statement. This means that
GetData
will only be called after you close the window.
Edit:
To prevent the user from messing with the main window, you could also kick off GetData on a background thread just before showing frmLoading as a dialog. This will block the UI thread while frmLoading is open, so you will have to programmatically close it just before updating your datagrid.
void button_Click(object sender, EventArgs e)
{
new Action(GetData).BeginInvoke(null, null);
this.Invoke(new Action<bool>(StartLoading), true);
}
void GetData()
{
this.Invoke(new Action<bool>(StartLoading), false);
this.Invoke(new Action(FillDataGrid));
}
void FillDataGrid()
{
}
Links you might find useful:
http://stackoverflow.com/questions/1130208/how-to-disable-the-parent-form-when-a-child-form-is-active[
^]
http://support.microsoft.com/kb/318607[
^]