I have a windows forms application that needs to contact a
web service. I want to do so asynchronously--and have done
so with few problems, in fact. The only problem is that, if
my connection times out, I want the process to abort. I also
want to make sure that I discard my resources properly. Will
the following code do the trick, or are there hidden "gotchas"
in it? What would be a better solution?
private delegate void SetComposerNameDelegate(string composerName);
private void SetComposerName(string composerName){
m_txtResult.Text = composerName;
}
private void m_btnGetComposer_Click(object sender, EventArgs args){
//Composer: the Composer web service
Composer compService = new Composer();
AsyncCallback cb = new AsyncCallback(GetComposerCallback);
IAsyncResult ar = compService.BeginGetName(3, null, null);
ar.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, 10), true);
if (ar.IsCompleted){
string composerName = compService.EndGetName(ar);
this.Invoke(new SetComposerNameDelegate(SetComposerName),
new object[]{composerName});
} else {
MessageBox.Show("Timed out!");
}
compService.Dispose();
}
eb
|