简体   繁体   English

BackgroundWorker或Task.WhenAll

[英]BackgroundWorker or Task.WhenAll

I am using a background service now to process a time consuming operation and same time showing progress in UI 我现在正在使用后台服务来处理耗时的操作,并同时在UI中显示进度

But now looking for some performance improvements in terms of time [since at present background worker took more time to complete the processings] 但是,现在希望在时间上实现一些性能上的改进(因为当前背景工作者花了更多时间来完成处理)

The current solution of Do_Work in background worker is like 后台工作者中Do_Work的当前解决方案如下

foreach(string _entry in ArrayList){

  //process the _entry(communicate to a service and store response in Db) and took almost 2-3 seconds for the completion
}

ArrayList contains almost 25000 records . ArrayList包含近25000条记录。 So almost 25000*3 = 75000 seconds time is required to do the processing now 因此现在需要大约25000 * 3 = 75000秒的时间来进行处理

The new solution i am thinking of is like starting 50 Threads of 500 items each at same time and waits for the completion of all Threads something like this 我正在考虑的新解决方案就像是同时启动500个项目的50个线程,并等待所有线程完成,如下所示

int failed = 0;
var tasks = new List<Task>();            
for (int i = 1; i < 50; i++)
{
    tasks.Add(Task.Run(() =>
    {

        try
        {
            //Process 500 items from Array .(communicate to a service and store response in Db)
        }
        catch
        {
            Interlocked.Increment(ref failed);
            throw;
        }
    }));
}
Task t = Task.WhenAll(tasks);    //Runs 50 Threads         
try
{
    await t;
}
catch (AggregateException exc)
{
string _error="";
    foreach (Exception ed in t.Exception.InnerExceptions)
    {
       _error+=ed.Message;
    }
     MessageBox.Show(_error);
}
if (t.Status == TaskStatus.RanToCompletion)
    MessageBox.Show("All ping attempts succeeded.");
else if (t.Status == TaskStatus.Faulted)
    MessageBox.Show("{0} ping attempts failed", failed.ToString());

Will this helps to reduce the processing time? 这会有助于减少处理时间吗? Or some better approaches? 还是一些更好的方法? I tried with a small sample size of 10 and debugged , but i cant see much difference (Something is wrong in my choice of WhenAll ?) 我尝试使用10的小样本样本进行调试,但是看不出太大差异(我在选择WhenAll时出了点问题?)

You need to handle few records at a time. 您一次只需要处理几条记录。

Process n records with n different tasks. 处理具有n个不同任务的n条记录。 Let's say n = 5. So 5 different tasks t1,t2,t3,t4 and t5. 假设n =5。所以有5个不同的任务t1,t2,t3,t4和t5。 Once anyone is completed processing data one row they should start processing t (1+n), t (2+n), t(3+n),t(4+n), t(5+n) row and so on until all records are processed. 一旦任何人完成一行数据的处理,他们就应该开始处理t(1 + n),t(2 + n),t(3 + n),t(4 + n),t(5 + n)等直到处理完所有记录。

Store all those processed values into a dictionary or list to identify which one belongs to which record. 将所有这些处理后的值存储到字典或列表中,以识别哪个属于哪个记录。

It's like recursive function. 就像递归函数一样。

I have used this kind of approach in past and it really improves the performance a lot. 我过去曾经使用过这种方法,它确实大大提高了性能。 You can fine-tune the value of n based on your PC configuration 您可以根据您的PC配置微调n的值

我建议尝试使用BlockingCollection

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

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