M
Michael Tumy
I have an application that could do some lengthy updates of a database upon
closing a form. So to keep the UI responsive, I'm pushing the updates off to
a worker thread. I seen numerous articles on using BeginInvoke on delgates
to keep you UI responsive. In these articles, they do a "WaitOne()" call on
the UI thread while the worker thread is processing the data.
This confuses me. How can the UI thread be responsive if it's blocked via
the WaitOne call? I've found that my UI does not redraw if I switch from
this app to another, then back while the update is in progress.
Do I have my code structured correctly? What I'm looking for is when the
"OK" button of the form is clicked, perform the update and then close the
form if the updated succeeded. Since this can take a long time, I need to
ensure that the user does not try to "Rush" the system by clicking the
"Cancel" or "X" buttons repeatedly, or change more data while the update is
in progress.
********** Begin Code *************
private void btnOK_Click(object sender, EventArgs e) {
this.Enabled = false; // Disable the form to prevent more data changes.
Cursor crOld = Cursor.Current;
try {
Cursor.Current = Cursors.WaitCursor;
UpdateDelegate dlgt = new UpdateDelegate(UpdateData);
IAsyncResult ar = dlgt.BeginInvoke(new AsyncCallback(UpdateCallback),
dlgt);
ar.AsyncWaitHandle.WaitOne();
} finally {
Cursor.Current = crOld;
}
}
private delegate void UpdateDelegate();
private int UpdateData() {
// Update the data, simulating a SLOW network connection.
System.Threading.Thread.Sleep(10000);
return (0);
}
private void UpdateCallback(IAsyncResult ar) {
UpdateDelegate dlgt = (UpdateDelegate) ar.AsyncState;
int nResult = dlgt.EndInvoke(ar);
// Switch back to the UI thread
this.Invoke(new UpdateCompleteDelegate(UpdateComplete), new object[]
{nResult});
}
private void UpdateCompleteDelegate(int result);
private void UpdateComplete(int result) {
if (result == 0) {
this.Close();
} else {
this.Enabled = true;
MessageBox.Show("Update failed. Error " + result.ToString());
}
}
*********** End Code *************
Thanks
closing a form. So to keep the UI responsive, I'm pushing the updates off to
a worker thread. I seen numerous articles on using BeginInvoke on delgates
to keep you UI responsive. In these articles, they do a "WaitOne()" call on
the UI thread while the worker thread is processing the data.
This confuses me. How can the UI thread be responsive if it's blocked via
the WaitOne call? I've found that my UI does not redraw if I switch from
this app to another, then back while the update is in progress.
Do I have my code structured correctly? What I'm looking for is when the
"OK" button of the form is clicked, perform the update and then close the
form if the updated succeeded. Since this can take a long time, I need to
ensure that the user does not try to "Rush" the system by clicking the
"Cancel" or "X" buttons repeatedly, or change more data while the update is
in progress.
********** Begin Code *************
private void btnOK_Click(object sender, EventArgs e) {
this.Enabled = false; // Disable the form to prevent more data changes.
Cursor crOld = Cursor.Current;
try {
Cursor.Current = Cursors.WaitCursor;
UpdateDelegate dlgt = new UpdateDelegate(UpdateData);
IAsyncResult ar = dlgt.BeginInvoke(new AsyncCallback(UpdateCallback),
dlgt);
ar.AsyncWaitHandle.WaitOne();
} finally {
Cursor.Current = crOld;
}
}
private delegate void UpdateDelegate();
private int UpdateData() {
// Update the data, simulating a SLOW network connection.
System.Threading.Thread.Sleep(10000);
return (0);
}
private void UpdateCallback(IAsyncResult ar) {
UpdateDelegate dlgt = (UpdateDelegate) ar.AsyncState;
int nResult = dlgt.EndInvoke(ar);
// Switch back to the UI thread
this.Invoke(new UpdateCompleteDelegate(UpdateComplete), new object[]
{nResult});
}
private void UpdateCompleteDelegate(int result);
private void UpdateComplete(int result) {
if (result == 0) {
this.Close();
} else {
this.Enabled = true;
MessageBox.Show("Update failed. Error " + result.ToString());
}
}
*********** End Code *************
Thanks