Synchronizing asynchronous HttpWebRequest

M

MaxMax

I'm using HttpWebRequest. It seems that all the callback called from
HttpWebRequest are in another thread (not in the "original" thread). Now my
problem is that the "original" thread is the thread that maintains the
interface (the forms). I want to signal from the "worker" thread to the main
thread that I've finished downloading the page. How should I do? I can't
simply wait for the end of the thread as in this
http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx
example (the example is a console application) otherwhise my interface would
be not-responsive. And clearly I don't want to use a timer-based polling
method.

--- bye
 
N

Nicholas Paldino [.NET/C# MVP]

MaxMax,

In this case, you want to call the Invoke method on a control on one of
your forms (or the forms themselves), passing a delegate and whatever
arguments you need passed to the delegate. This will cause the delegate to
be invoked on the thread that created the control.

Hope this helps.
 
M

MaxMax

In this case, you want to call the Invoke method on a control on one of
your forms (or the forms themselves), passing a delegate and whatever
arguments you need passed to the delegate. This will cause the delegate
to be invoked on the thread that created the control.

Hope this helps.
Yep... Now another related question: how does the SoundPlayer do? If you use
LoadAsync he is asynchronous BUT (and I've tested with a wav from an http
file) the event is fired in the main thread AND the SoundPlayer class
doesn't have any handle to the main thread. I've used this example:
http://msdn2.microsoft.com/en-us/library/w50sk4c9.aspx

Thanks!
 
W

Walter Wang [MSFT]

Hi,

SoundPlayer internally uses Event-based Asynchronous Pattern
(http://msdn2.microsoft.com/en-us/library/wewwczdw(VS.80).aspx).

Currently not all library classes that support asynchronous operations
follow this new pattern for asynchronous execution. One such example is
HttpWebRequest, and SqlCommand.

Fortunately, it's not too difficult to use this pattern for these classes,
as Mike Woodring described:

#SqlCommand.BeginExecuteXxx, SynchronizationContext, and Anonymous Methods
http://pluralsight.com/blogs/mike/archive/2005/11/29/17198.aspx


Although Mike's helper class named SqlAsyncOperation, it's actually a
general one and can be used for the HttpWebRequest:

private HttpWebRequest request;
private void button1_Click(object sender, EventArgs e)
{
request = (HttpWebRequest)
WebRequest.Create("http://msdn.microsoft.com");
// request.BeginGetResponse(new AsyncCallback(myCallback),
null);
SqlAsyncOperation.Start(request.BeginGetResponse, myCallback,
null);
}

void myCallback(IAsyncResult ar)
{
HttpWebResponse response = (HttpWebResponse)
request.EndGetResponse(ar);
this.Text = "Done";
}


Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top