views:

46

answers:

1

I am performing an http web request which asynchronously gets a response from the server. I wish to perform actions on the result directly and do not wish to have any code running in the meanwhile. The reason it has to be asynchronous is that I am writing a silverlight application.

Here is a code snippet

{
....

request.BeginGetResponse(new AsyncCallback(Callback), request);

//Some UI Code to be done after the callback 
}

private void Callback(IAsyncResult asynchronousResult)
{
//Code needed to be done before the UI code 
}

But as soon as it reaches the async request it skips the callback and return to the calling function.

Is there any way to be able to wait for the asynchronous request. I have tried using WaitOne() on the asynchronous result but it did not solve the problem.

Thanks for the help!

+1  A: 

You could do this:

        IAsyncResult ar = request.BeginGetResponse(new AsyncCallback(Callback), request);
        ar.AsyncWaitHandle.WaitOne(5000);

In this particular case I am using the timed out version of WaitOne() which returns true if the wait was successful. You could use any of the WaitOne() overloads though

Steve Ellinger
It still gives the same result. It never comes back to callback function. It just continues with the statements in the calling function.
Roshan