I am making a WPF app that needs to download multiple files from online. Currently, when I call the event which leads to the file download, my app hangs for a while as it downloads the file and only returns to functioning when the download is complete. Is there a way to make it such that the file downloads in the background and the application doesn't hang? I'm relatively new at this so the most information possible would be excellent. Thank you!
Edit: I should have been more clear. I have a function called downloadFile()
, and when the main event is triggered, I need to download multiple files. Here is my code:
private void DownloadAll()
{
foreach(string moddiekey in moddiekeys)
{
download_file_by_moddiekey(moddiekey );
}
}
private async void Event(){
await Task.Run(() => { DownloadAll(); });
}
But this still hangs my application.
Edit 2: Showing download functions
private void download_file_by_url(string url)
{
try
{
string[] splits = url.Split('/');
string filename = splits[splits.Length - 1];
WebClient wc = new WebClient();
wc.DownloadFile(url, filename);
wc.Dispose();
}
catch { }
}
private void download_file_by_moddiekey(string moddiekey)
{
...
download_file_by_url("github link/" + moddict[moddiekey]);
...
}