how can i await void of downloading in alttohttp library in c# .net?
when I was using WebClient it was working to make await before the code and it awaits to finish the download but in Alttohttp library it doesn't await due to it's a void.
this is the code :
httpdownloader = new HttpDownloader(uri, filename);
httpdownloader.DownloadCompleted = htppdownloaderU_Completed;
httpdownloader.ProgressChanged = httpdownloaderU_Progress;
httpdownloader.StatusChanged = httpdownloader_StatusChanged;
await httpdownloader.Start(); // then after finishing the whole download
i want this code to continue the void code after finishing the download by this library
CodePudding user response:
If I understand correctly, you want to excecute something after the httpdownloader is done. If so, you can add your code in httpdownloaderU_Completed which is a delegate function that will be excecuted after your download is completed. Something like this:
void httpdownloaderU_Completed(object sender, EventArgs e)
{
MessageBox.Show("Download comleted!");
...
}
CodePudding user response:
I think you misunderstand how the library work. It is event-driven, so the only way to know if the download is complete is to check when the code enters the htppdownloaderU_Completed
method. if you really want to continue after calling the Start()
method, maybe you can have a global variable for the class that you set in that Method like
IsDownloadComplete = true;
and then instead of trying to do this:
await httpdownloader.Start();
you can do a while loop to wait for the download to complete. something like
httpdownloader.Start();
while(IsDownloadComplete == false)
{
await Task.Delay(1000);
}
// Download is now complete, do something with the download
DoSomething();
but is probably not the most ideal way, and you should probably rethink, and just do everything you want to do after the download is complete from the htppdownloaderU_Completed
method