Task.wait not working as I imagined
- by user2357446
I am trying to download a file, wait for the file to finish downloading, and then read the file afterwards. I have the following methods to do this:
private async Task startDownload(string link, string savePath)
{
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
await client.DownloadFileTaskAsync(new Uri(link), savePath);
}
private void checkUpdateButton_Click(object sender, EventArgs e)
{
Task task = Task.Factory.StartNew(() => startDownload(versionLink, versionSaveTo));
task.Wait();
if (task.IsCompleted)
{
checkVersion();
}
}
The checkVersion() method reads the file that was downloaded. This is throwing an IOException saying that the file is in use by something else and cannot be read. I thought that having task.Wait would prevent the rest of the method from executing until the task was finished?