I'm trying to automate a process of logging in to a website and posting and
downloading some stuff. I am using asynchronous WebClient methods so the
user can cancel the operation at any time and it's working fine, but I have a
question about the structure of my program. Right now I've got something
like this:
Main()
{
WebClient LoginClient = new ...
// set the relevant properties and eventhandlers
LoginClient.UploadValuesAsync(...);
}
LoginClient_UploadValuesCompleted(...)
{
if(!Cancelled && !Error)
{
WebClient Page1Client = new ...
// set the relevant properties and eventhandlers
Page1Client.DownloadDataAsync(...);
}
}
Page1Client_DownloadDataCompleted(...)
{
if(!Cancelled && !Error)
{
WebClient Page2Client = new ...
// set the relevant properties and eventhandlers
Page2Client.DownloadDataAsync(...);
}
}
etc.
I've got about 5 or 6 pages I need to get and I have to get them in order,
so in each request's completed event handler I start the next request. Is
this the right way to do things? There seems to be a fair amount of repeated
code and it just feels a little strange, but it works fine.
Also, whenever I start the program and start the first request for the first
time it blocks for about a second even though it's started asynchronously.
No other requests block, even if I cancel and restart the request sequence.
If I restart the program, the first request blocks again. Is this just
because some initialization occurs in the background or something? Is there
any way to avoid this?
|