1

Can someone please help me with this here is my code, I want to make the unzip task wait for the file to download but its not an and it doesn't let me await a void, Ive looked everywhere and i cant figure it out so can someone send me back the working code thanks (note all this code works but not with async):

        WebClient client = new WebClient();
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.DownloadFileAsync(new Uri("https://webserver-test-1.000webhostapp.com/spacelightzipped.zip"), Environment.CurrentDirectory + "\\spacelightzipped.zip");

        String ZipPath = Environment.CurrentDirectory + "\\spacelightzipped.zip";
        String extractPath = Environment.CurrentDirectory;
        ZipFile.ExtractToDirectory(ZipPath, extractPath);

        System.Diagnostics.Process proc = new System.Diagnostics.Process
        {
            EnableRaisingEvents = false
        };
        proc.StartInfo.FileName = Environment.CurrentDirectory + "\\SpaceLightApp.exe";
        proc.Start();
Tristan Read
  • 61
  • 10
  • _"it doesn't let me await a void"_ You can use just Task instead of void, so that you can await in the place where you are invoking it. – Thangadurai Jun 23 '18 at 13:11
  • 1
    @Thangadurai - I think the OP is referring to [`DownloadFileAsync`](https://msdn.microsoft.com/en-us/library/ms144196(v=vs.110).aspx) – Damien_The_Unbeliever Jun 23 '18 at 13:12
  • If you want to see all of my code it is [here](https://webserver-test-1.000webhostapp.com/codeasyncandwait.txt) – Tristan Read Jun 23 '18 at 13:13
  • You may wish to look at [`DownloadFileTaskAsync`](https://msdn.microsoft.com/en-us/library/hh193917(v=vs.110).aspx) instead. – Damien_The_Unbeliever Jun 23 '18 at 13:13
  • @Damien_The_Unbeliever, Ah..sorry I missed that point. This [SO](https://stackoverflow.com/questions/16514027/download-multiple-files-async-and-wait-for-all-of-them-to-finish-before-executin) post provides a workaround. – Thangadurai Jun 23 '18 at 13:18
  • @Thangadurai i tried that but I get new errors, can someone like edit it so that it works thx [screenshot](https://www.dropbox.com/s/ysfm4xlm7w9qze6/Screenshot%202018-06-23%2014.21.13.png?dl=0) – Tristan Read Jun 23 '18 at 13:20
  • Please EDIT your question and rephrase exactly what is your issue. As it is described right now, it is quite unclear what you need. – sɐunıɔןɐqɐp Jun 23 '18 at 13:36

1 Answers1

0

You have to use the event WebClient.DownloadFileCompleted Event which will be raised when the file is completely downloaded, then execute whatever code you want on the file.

So you need to register the event for your webClient. like :

client.DownloadFileCompleted += wc_DownloadFileCompleted;

Then call your code to extract and execute in the DownloadFileCompleted event.

private static void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            String ZipPath = Environment.CurrentDirectory + "\\spacelightzipped.zip";
            String extractPath = Environment.CurrentDirectory;
            ZipFile.ExtractToDirectory(ZipPath, extractPath);

            System.Diagnostics.Process proc = new System.Diagnostics.Process
              {
                 EnableRaisingEvents = false
              };
             proc.StartInfo.FileName = Environment.CurrentDirectory + "\\SpaceLightApp.exe";
             proc.Start();
        }
Kaj
  • 806
  • 6
  • 16