当我使用DownloadFileAsync
时,它似乎“阻止”了某些东西。我需要程序能够在文件下载时下载更多字符串(下载文件,但用户仍然能够在目录中搜索链接以下载更多文件)。
UI 不是 100% 被阻止的,但是当用户单击“搜索”按钮时,它无法正常工作,也没有DataGridView
处理中的单击。然而,搜索按钮会清除DataGridView
编程的内容,但是我编写的将目录下载为字符串(与 异步DownloadStringTaskAsync
)的等待内容不起作用。但是,当下载完成时,搜索最终会通过然后填充DataGridView
,这对我来说似乎是非常不正常的行为。
当我注释掉 时DownloadFileAsync
,一切都能够再次正常执行。我还尝试注释掉我已经放置的事件处理程序,但这也不能解决问题。我不确定,感谢您的帮助。
一些代码片段:
下载文件:
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);
};
搜索目录,该目录由主窗体上的按钮调用:
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;
}
}