Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
下载文件异步';区块';在c#中?_C#_Webclient_Downloadfileasync - Fatal编程技术网

下载文件异步';区块';在c#中?

下载文件异步';区块';在c#中?,c#,webclient,downloadfileasync,C#,Webclient,Downloadfileasync,当我使用DownloadFileAsync时,它似乎“阻止”了一些东西。我需要的程序能够下载更多的字符串,而文件下载(下载一个文件,但用户仍然能够搜索目录的链接下载更多的文件) UI没有100%被阻止,但当用户单击“搜索”按钮时,它不能正常工作,也不能在DataGridView中进行单击。不过,搜索按钮会按编程方式清除DataGridView,但我编写的等待操作(与DownloadStringTaskAsync异步)不起作用。然而,当下载完成后,搜索最终通过并填充DataGridView,这在我

当我使用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);
        };
var bmclient=new WebClient();
bmclient.DownloadFileAsync(新Uri(downloadURL)、Path.Combine(Application.StartupPath、originalFileName+“.nexd”);
bmclient.DownloadProgressChanged+=(o,e)=>
{
int rowIndex=-1;
DataGridViewRow行=form1.dataGridView2.Rows
.Cast()
.Where(r=>r.Cells[0]。Value.ToString().Equals(setID))
.First();
rowIndex=行索引;
MethodInvoker操作=()=>form1.dataGridView2[2,rowIndex].Value=e.ProgressPercentage.ToString()+“%”;
表1.开始行动(行动);
};
正在搜索目录,该目录由主窗体上的按钮调用:

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;
        }
    }
公共静态异步任务GetBloodcatSearch(字符串查询)
{
var return_data=string.Empty;
尝试
{
使用(var client=new WebClient())
{
return\u data=wait client.downloadstringtaskancy(新Uri(“目录/”+query));
}
}
捕获(例外e)
{
返回null;
}
}

你能展示一些代码让我们知道你是如何使用DownloadFileAsync的,或者你的搜索是如何工作的吗?@Nicholas Tay请展示代码问题在
DownloadFileAsync
的第五行,你看到了吗?我添加了一些代码。(@stenportov what的第五行,我不确定你指的是什么。)如果没有一个好的答案,就不可能确定你的具体问题是什么。同时,我建议您在开始操作之前订阅
DownloadProgressChanged
事件。此外,您需要确保UI对象的所有访问都发生在UI线程中;您的
DownloadProgressChanged
处理程序使用例如
form1.dataGridView2
,在我看来它就像一个UI对象。可能整个方法体都应该在被调用的匿名方法中。