ending an async call to a web service

V

VR

I am trying to access a web service from a Windows Forms
based app, and I need to make the call to the service
asynchronously, so that a user could stop the call using
UI controls.

I implemented it in MyForm, that has a cancel button --
btnCancel. Is it possible to make sure that in
btnCancel_Click function I can do something that would
prevent the callback event from firing?

Here is the code:

void AccessWebService()
{
AsyncCallback cb = new AsyncCallback(MyCallback);

int iData = 1;

m_oAsyncResult = m_oWebService.BeginAccessWeb(
iData,
cb,
m_oWebService);

}


void MyCallback(IAsyncResult oResult)
{
CWebService oWebService =
(CWebService)oResult.AsyncState;

int iResultData = oWebService.EndAccessWeb(oResult);
}


void btnCancel_Click(object sender, EventArgs e)
{
// ?????
// what can I do here to prevent MyCallback
// from being called?

this.Hide();
}

What can stop MyCallback from being called? Also, I am not
very sure how thread-safe it is to access m_oAsyncResult
in btnCancel_Click(), are btnCancel_Click() and
MyCallback serialized or would MyCallback will be called
from a context of a different thread?

Thanks for any suggestions.

VR
 
A

Arthur Nesterovsky

Hi,

Insert the following code into the btnCancel_Click method:

WebClientAsyncResult asyncResult = m_oAsyncResult as
WebClientAsyncResult;
if (asyncResult != null && !asyncResult.IsCompleted)
{
asyncResult.Abort();
}
Also, I am not very sure how thread-safe it is to access m_oAsyncResult
in btnCancel_Click(), are btnCancel_Click() and
MyCallback serialized or would MyCallback will be called
from a context of a different thread?

If you are not sure in something then do lock() around that object.
It sounds like a life principle. ;-)
______________________________
With best wishes, Arthur Nesterovsky
Please visit my home page:
http://www.nesterovsky-bros.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top