Hi,
I have a windows forms app, and a web service that can take a long time to complete, so i use it asynchronously with the proxy generated Begin/End methods.
However, i want the user to be able to cancel the operation. i understand that the request has already been sent off, and the next thing we'll hear back is when the result is received, so an actualy cancel will be difficult to achieve. I have tried calling webService.Abort() but this throws a ThreadException. I have worked out the following crude work-around, which is simply to ignore the result when it comes in, by means of a simple bool variable which gets set to 'true' (meaning ignore) when btnCancel is clicked.
I would be interested to hear if anyone else has a better solution. my code fragment is below.
many thanks
tim mackey.
private bool ignoreResult = false;
private ws.Service1 webService;
....
private void btnStart_Click(object sender, System.EventArgs e)
{
AsyncCallback callBack = new AsyncCallback(Callback);
IAsyncResult result = webService.BeginHelloWorld(5, callBack, null);
}
public void Callback(IAsyncResult asyncResult)
{
if(ignoreResult)
return;
this.label1.Text = webService.EndHelloWorld(asyncResult);
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
ignoreResult = true;
this.label1.Text = "Cancelled
}
\\ email: tim at mackey dot ie //
\\ blog: http://tim.mackey.ie //
67d0ebfec70e8db3