When I use DownloadFileAsync
, it seems to 'block' something. I need the program to be able to download more strings while the file is downloading (downloading a file, but user is still able to search a directory for links to download more files).
The UI is not 100% being blocked, but when the user clicks the 'search' button it doesn't work properly, nor do clicks in the DataGridView
get handled. The search button however clears the DataGridView
as programmed, but the await thing that I wrote to download the directory as a string (asynchronously with DownloadStringTaskAsync
) does not work. However, when the download finishes, the search finally goes through and then populates the DataGridView
, which seems like very abnormal behavior to me.
When I comment out the DownloadFileAsync
, everything is able to perform normally again. I have also tried to comment out the event handlers that I have put in place, but this also does not fix the issue. I am not sure, thanks for any help.
Some code snippets:
Downloading the file:
var bmclient = new WebClient();
bmclient.DownloadFileAsync(new Uri(downloadURL), Path.Combine(Application.StartupPath, originalFileName + ".nexd"));
bmclient.DownloadProgressChanged += (o, e) =>
{
int rowIndex = -1;
DataGridViewRow row = form1.dataGridView2.Rows
.Cast<DataGridViewRow>()
.Where(r => r.Cells[0].Value.ToString().Equals(setID))
.First();
rowIndex = row.Index;
MethodInvoker action = () => form1.dataGridView2[2, rowIndex].Value = e.ProgressPercentage.ToString() + "%";
form1.BeginInvoke(action);
};
Searching the directory, which is being called by a button on the main form:
public static async Task<string> GetBloodcatSearch(string query)
{
var return_data = string.Empty;
try
{
using (var client = new WebClient())
{
return return_data = await client.DownloadStringTaskAsync(new Uri("directory/" + query));
}
}
catch (Exception e)
{
return null;
}
}