Asynchronous WebRequest

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I use hpptwebrequest to download files from my web server to my client
machines.
I am attempting to convert my code to asychronous operation. Can somebody
in simple language explain what the purpose of RespCallBack, ReadCallBack and
IAsyncResult?

Thanks,
Fred
 
In plain english eh...

The process of connecting to a web-page is multi-part. First, you establish a
request. Which
you are obviously doing. GetResponse and BeginGetResponse fire off this process
for you.
Now, GetResponse will hang your UI until you get a response from the server so
you'll want
to use the BeginGetResponse version.

That puts us to step 2. BeginGetResponse requires a call-back, which is where
you get your
RespCallback from. This method is called by the net classes when the data is
available. This
all happens on another thread and is thus asynchronous. This is great for you,
because after
your calling of BeginGetResponse your program can continue to interact with the
user.

That puts you to step 3. You are now in the callback method. IAsyncResult
identifies the
original call that you made. Remember here that many asynchronous operations can
be
happening at the same time (different threads) and that your callback method has
to
differentiate between them. IAsyncResult is also used by the net classes to
retrieve the
information associated with the entire call process. Serves a dual purpose and
is very
important.

At this point, you are still in the callback, on a different thread, and you can
choose to
end the request. This means getting the returned information and then figuring
some way
to display it back to the user. Remember, different thread still, so it isn't
potentially safe to
update the UI directly. You also wind up with another option, and that is where
your ReadCallBack
comes from. Not only do the net classes support the connection and response
process asynchronously
they also allow you to read the data back that the server is sending
asynchronously. This isn't
nearly as important for you as the first step. Instead forget about reading the
file asynchronously and
use the thread you are currently on to read the data synchronously. This is not
going to hose your
UI because you are on a separate thread from the UI thread.

In short:
BeginGetResponse -> Puts you on another thread
RespCallback -> Notifies you data is ready
IAsyncResult -> Uniquely identifies this request from others
ReadCallback -> Used to implement additional asynchronous processing of returned
data. Not really needed in most cases.
 
Ok, you are telling me not to use readcallback but to read the stream data
from inside the respcallback thread???

Fred
 
Yes. By reading synchronously you drastically reduce the complexity of your
application and because you are already on another thread you ensure you are
also asynchronous. Be aware that a potential issue is thread starvation, but the
application of some sleep calls while reading your data synchronously would
get rid of that quite easily.
 
Hi Justin, thanks for you explainations and help. I got asynchronous http to
work. It is both fasinating and wierd. I find that with a few clicks I can
stack up 50 megabyes of downloads and just have no idea where my program is.
I guess that I will need to go back to normal mode where I can keep my
operations in the proper sequence. I guess not every neat feature is
practical.

Fred
 
You need download management at this point. Remember that you can store custom
state information on each request. By doing this you can keep a central
repository of
requests, their current status, and whether or not they are complete. This isn't
100%
obvious in the beginning when you first start throwing off new requests like a
mad-man ;-)
 
Thankyou Justin. Perhaps I am being impatient in abandoning the asychronous
approach. I guess the smart tihing to do now is launch a help session with
MS and get some coaching in state management and see what is really
possilble with this approach.

Thanks so much for your comments. They were very useful to me.

Fred
 
Back
Top