0

The download function downloads async so that the UI doesn't freeze, but I need it to download one file at a time, so I have a second function called downloadall() that needs to call the download function, then wait fo the file to be downoaded and installed (this is a bool variable), then call the download function again

public void Download()
{
  using (WebClient wc = new WebClient())
  {
    wc.Credentials = new NetworkCredential(User, Password);
    wc.DownloadProgressChanged += wc_DownloadProgressChanged;
    wc.DownloadFileAsync
    (
      new System.Uri(DownloadURL),
      path + Name + ".zip"
    );
  }
}
        private void DownloadAllBtn_Click(object sender, EventArgs e)
        {
            foreach (Pakage p in Program.Pakages)
            {
                if (!p.IsInstalled)
                {
                    Download(p);
                }
                while (!p.IsInstalled)
                {
                    ///wait without freezing UI
                }
            }
        }
Bananaboy
  • 11
  • 2
  • Consider using HttpClient (instead of WebClient) and doing everything with Async and Await. Async and Await *really* go well with UI apps (like WinForms and WPF) – Flydog57 Mar 15 '21 at 18:14
  • I can't use HttpClient because I also use this to download via FTP – Bananaboy Mar 15 '21 at 18:42
  • Look into TaskCompletionSource (https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskcompletionsource-1) and convert your `Download` method into an awaitable function (that returns a Task). – Flydog57 Mar 15 '21 at 19:31

0 Answers0