I need to Download a File from a Https Source.
I’ll do this asynchron like this (works so far):
void doChecksbeforDownload(){
//Do some Checks
DownloadFileAsync();
}
void DownloadFileAsync(){
...
...
this.client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
this.client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
this.client.UploadStringAsync(new Uri(url), "POST", PostParameter);
...
...
}
and call the client_UploadStringCompleted() Method when finished:
void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
//Do Check here
}
So far so good. Now I put all this in a class “Functions” and call the Method like this:
Functions f = new Functions();
f.doChecksbeforeDownload();
I want doChecksbeforeDownload() to wait until the clientUloadStringCompleted is FINISHED.
How do I do I tell doChecksbeforeDownload to wait until the the Async call in DownloadFilesAsync is done and ready.
- Call doChecksbeforeDownload()
- ChecksbeforeDownload()->DownloadFileAsync()
- ChecksbeforeDownload()->Waits…….
- DownloadFileAsync() -> Completet & Ready
- ChecksbeforeDownload()->returns FOO to Main Class
Are there any best practices / examples to achieve this? I stuck in this point.
Thanks in advance
Hannes
You will want to use the synchronization objects exposed in .NET.
Check out this link. Here’s an excerpt:
NOTE: Be careful making your reset events static, etc. Then you’ll be introducing thread safety issues. The above example is only static for simplicity.
In your case, you’d want to make to make the autoreset event a member of your class that is performing the asynchronous. In your function, after you start the asynchronous call, wait on your handle. In the completion event, set your event which should unblock your wait handle.
Consider that you may want to introduce timeouts for the call to WaitOne(), etc.