简体   繁体   English

用C#一次执行任务列表

[英]Executing a list of tasks at one shot in C#

I am trying to execute a list of tasks at runtime. 我正在尝试在运行时执行任务列表。 I want this to be an async operation. 我希望这是一个异步操作。 Each task is invoking a webservice and returning a response which i am adding to a List. 每个任务都在调用一个Web服务并返回我正在添加到列表中的响应。 When all the operations are completed, i will merge all into a single doc. 当所有操作完成后,我将全部合并到一个文档中。

Here is my code. 这是我的代码。 Currently it is executing as just sync calls only. 当前,它仅作为同步调用执行。 I want them to be shot at one times. 我希望他们一次被枪杀。

List<XmlDocument> XmlResponse = new List<XmlDocument>();
XmlDocument xDocument = new XmlDocument();
ServiceRequest service = GetServiceRequest();
var tasks = new List<Task>();

Parallel.ForEach(service.IRN, x => 
{
    tasks.Add(Task.Factory.StartNew(() =>
    {
        XmlDocument xRequest = BuildServiceRequest(
            new ServiceRequest 
            {
                AccountNumber = service.AccountNumber, 
                MemberId = service.MemberId, 
                IRN = new List<string> { x }
            });
        Debug.Print("Service Call " + DateTime.Now.ToString("hh.mm.ss.ffffff"));
        XmlDocument xResponse = WebRequest.Submit(xRequest); // This is invoking service
        XmlResponse.Add(xResponse);
    }));
});

try
{
    Task.WaitAll(tasks.Where(x => x != null).ToArray());
    xDocument = PrepareServiceResponse();
    Debug.Print("Task Ends" + DateTime.Now.ToString("hh.mm.ss.ffffff"));
}

If I run this code now, the service is invoked synchronously. 如果我现在运行此代码,该服务将被同步调用。 Can anyone suggest the way to shoot all calls at one time in tasks list? 谁能在任务列表中建议一次拍摄所有电话的方式?

You seem to be creating a load of tasks INSIDE a Parallel ForEach loop. 您似乎正在并行ForEach循环中创建大量任务。 You don't need to do that, as the Parallel.ForEach handles the creation of the tasks for you. 您不需要这样做,因为Parallel.ForEach会为您处理任务的创建。

Parallel.ForEach(service.IRN, x => 
{
    XmlDocument xRequest = BuildServiceRequest(
       new ServiceRequest 
        {
            AccountNumber = service.AccountNumber, 
            MemberId = service.MemberId, 
            IRN = new List<string> { x }
        });
    Debug.Print("Service Call " + DateTime.Now.ToString("hh.mm.ss.ffffff"));
    XmlDocument xResponse = WebRequest.Submit(xRequest); 
    XmlResponse.Add(xResponse);
});

That should do it. 那应该做。 Once you get to the next line all tasks will have completed. 一旦到达下一行,所有任务都将完成。 However, depending on how many calls you are making, how many threads are used, and how long each call takes there is no guarantee that the responses will be in the same order as the IRN strings. 但是,根据您进行的调用次数,使用的线程数以及每个调用花费的时间,不能保证响应的顺序与IRN字符串的顺序相同。

As an aside, you might get errors adding to the list. 顺便说一句,添加到列表中可能会出错。 See these two questions for alternative solutions: Can we add new elements to a list using Parallel.ForEach()? 有关替代解决方案,请参见以下两个问题:是否可以使用Parallel.ForEach()将新元素添加到列表中? or Is this use of Parallel.ForEach() thread safe? 还是使用Parallel.ForEach()线程安全吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM