Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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# Task.WaitAll被卡住了_C#_Task Parallel Library - Fatal编程技术网

C# Task.WaitAll被卡住了

C# Task.WaitAll被卡住了,c#,task-parallel-library,C#,Task Parallel Library,我有一段代码如下所示: var taskList = new Task<string>[masterResult.D.Count]; for (int i = 0; i < masterResult.D.Count; i++) //Go through all the lists we need to pull (based on master list) and create a task-list { using (va

我有一段代码如下所示:

    var taskList = new Task<string>[masterResult.D.Count];
    for (int i = 0; i < masterResult.D.Count; i++)        //Go through all the lists we need to pull (based on master list) and create a task-list
    {
        using (var client = new WebClient())
        {
            Task<string> getDownloadsTask = client.DownloadStringTaskAsync(new Uri(agilityApiUrl + masterResult.D[i].ReferenceIdOfCollection + "?$format=json"));
            taskList[i] = getDownloadsTask;
        }
    }

    Task.WaitAll(taskList.Cast<Task>().ToArray());      //Wait for all results to come back
var taskList=新任务[masterResult.D.Count];
for(int i=0;i

任务完成后代码冻结。WaitAll。。。我有一个想法,为什么,这是因为客户在打电话时已经处理好了,有可能推迟到以后再处理吗?您能推荐另一种方法吗?

我不知道该代码如何工作,因为您在运行任务之前处理了
WebClient

您希望执行以下操作:

var taskList = new Task<string>[masterResult.D.Count];
for (int i = 0; i < masterResult.D.Count; i++)        //Go through all the lists we need to pull (based on master list) and create a task-list
{
    var client = new WebClient();
    Task<string> task = client.DownloadStringTaskAsync(new Uri(agilityApiUrl + masterResult.D[i].ReferenceIdOfCollection + "?$format=json"));
    task.ContinueWith(x => client.Dispose());
    taskList[i] = task;
}

Task.WaitAll(taskList.Cast<Task>().ToArray());      //Wait for all results to come back
var taskList=新任务[masterResult.D.Count];
for(int i=0;iclient.Dispose());
任务列表[i]=任务;
}
Task.WaitAll(taskList.Cast().ToArray())//等待所有结果返回
i、 e.如果您在第一个循环中处理
WebClient
,则当您使用
Task.WaitAll
触发任务时,它不会被分配。任务完成后将调用
ContinueWith
调用,因此可用于处理每个
WebClient
实例


但是,要让代码执行对单个主机的并发请求,您需要配置服务点。阅读此问题:

我不知道该代码将如何工作,因为您在任务运行之前处理了
WebClient

您希望执行以下操作:

var taskList = new Task<string>[masterResult.D.Count];
for (int i = 0; i < masterResult.D.Count; i++)        //Go through all the lists we need to pull (based on master list) and create a task-list
{
    var client = new WebClient();
    Task<string> task = client.DownloadStringTaskAsync(new Uri(agilityApiUrl + masterResult.D[i].ReferenceIdOfCollection + "?$format=json"));
    task.ContinueWith(x => client.Dispose());
    taskList[i] = task;
}

Task.WaitAll(taskList.Cast<Task>().ToArray());      //Wait for all results to come back
var taskList=新任务[masterResult.D.Count];
for(int i=0;iclient.Dispose());
任务列表[i]=任务;
}
Task.WaitAll(taskList.Cast().ToArray())//等待所有结果返回
i、 e.如果您在第一个循环中处理
WebClient
,则当您使用
Task.WaitAll
触发任务时,它不会被分配。任务完成后将调用
ContinueWith
调用,因此可用于处理每个
WebClient
实例


但是,要让代码执行对单个主机的并发请求,您需要配置服务点。阅读此问题:

您需要在任务中创建和处理WebClient。我没有办法测试这一点,但看看是否为您指明了正确的方向:

    var taskList = new Task<string>[masterResult.D.Count];
    for (int i = 0; i < masterResult.D.Count; i++)        //Go through all the lists we need to pull (based on master list) and create a task-list
    {
        taskList[i] = Task.Run(() =>
        {
            using (var client = new WebClient())
            {
                return client.DownloadStringTaskAsync(new Uri(agilityApiUrl + masterResult.D[i].ReferenceIdOfCollection + "?$format=json"));

            }
        });
    }

    Task.WaitAll(taskList.Cast<Task>().ToArray());  
var taskList=新任务[masterResult.D.Count];
for(int i=0;i
{
使用(var client=new WebClient())
{
返回client.downloadstringtasksync(新Uri(agilityapirl+masterResult.D[i].ReferenceIdOfCollection+“?$format=json”);
}
});
}
Task.WaitAll(taskList.Cast().ToArray());

您需要在任务中创建和处置WebClient。我没有办法测试这一点,但看看是否为您指明了正确的方向:

    var taskList = new Task<string>[masterResult.D.Count];
    for (int i = 0; i < masterResult.D.Count; i++)        //Go through all the lists we need to pull (based on master list) and create a task-list
    {
        taskList[i] = Task.Run(() =>
        {
            using (var client = new WebClient())
            {
                return client.DownloadStringTaskAsync(new Uri(agilityApiUrl + masterResult.D[i].ReferenceIdOfCollection + "?$format=json"));

            }
        });
    }

    Task.WaitAll(taskList.Cast<Task>().ToArray());  
var taskList=新任务[masterResult.D.Count];
for(int i=0;i
{
使用(var client=new WebClient())
{
返回client.downloadstringtasksync(新Uri(agilityapirl+masterResult.D[i].ReferenceIdOfCollection+“?$format=json”);
}
});
}
Task.WaitAll(taskList.Cast().ToArray());


它没有完全运行,这是正确的,它在尝试时会冻结。不是我,但我将尝试您的解决方案和+1 if works-1作为答案的注释,但即使现在,这也不是问题所在,这不是您建议中的好代码。WebClient可以而且应该被重复用于多个请求。@weston,WebClient不支持并发请求,我已经想到了这种方法。也许你可以在任务中处理客户端。继续,而不是保留单独的列表,对吗?它没有,完全运行,这是正确的,它尝试时会冻结。不是我,但我将尝试您的解决方案,如果+1有效,请使用+1作为答案,但即使是现在,这也不是问题所在,而且您的建议中的代码也不好。WebClient可以而且应该被重复用于多个请求。@weston,WebClient不支持并发请求,我已经想到了这种方法。也许你可以在任务中处理客户端。继续,而不是保留单独的列表,对吗?好吧,测试一下假设:当使用
被移动到其他所有内容时会发生什么?这是否确认或拒绝“idea”?它是否在异步函数中运行?它是异步的吗?外部代码是什么?重要的是,它在整个调用堆栈中都是异步的。不,函数是同步的。由于调用了
Task.WaitAll
,所以外部方法可能是不相关的(肯定不是异步的)。好吧,测试一下假设:当
usin