简体   繁体   English

创建多个线程并等待它们全部完成,然后再次调用该线程

[英]Create multiple threads and wait for them all to complete, calling again for the complete

I have a Web Service, to make the load of the database server for a local database, making 100 requests for records. 我有一个Web服务,可以为本地数据库加载数据库服务器,并发出100个记录请求。

Since the process is slow, I want to create ten threads, not to use too much memory, making Web Service calls, and when one of the threads, finished, over 100 call records. 由于该过程很慢,因此我想创建10个线程,而不要使用过多的内存来进行Web Service调用,并且当其中一个线程完成时,将创建100多个调用记录。 How do part of the thread? 线程的一部分如何?

Example: 例:

Create thread 1 Create thread 2 Create thread 3 Create thread 4 创建线程1创建线程2创建线程3创建线程4

thread 1 complete change Web Service again 线程1重新完成更改Web服务

Edit 编辑

My code not working. 我的代码无法正常工作。 Variable send always gets the value 10 and not 0,1,2,3,4 and etc. 变量send始终获取值10,而不是0、1、2、3、4等。

Int32 page = 0;            
do
{                
    for (int iterator=0; iterator < 10; iterator++)
    {                    
        listTask[iterator] = Task.Factory.StartNew(() =>
        {
            Int32 send = iterator + page * 10;
            DoStatus("Page: " + send.ToString());
            Processamento(parametros, filial, send);                        
        });
    }

    Task.WaitAll(listTask);
    page++;

}
while (true); // Test only

You're closing over the loop variable. 您正在关闭循环变量。 You need to remember that lambdas close over variables not over values . 您需要记住,lambda关闭的是变量而不是 Your tasks will each read the value of iterator at the time that the lambda executes iterator + page * 10 . 在lambda执行 iterator + page * 10 时,您的任务将各自读取iterator的值。 By the time that that happens the main thread has already incremented it to 10 . 到发生这种情况时,主线程已将其增加到10

This is simple enough to resolve. 这很容易解决。 Make a copy of the loop variable inside of your for loop so that the closure closes over that variable, which never changes. for循环内复制循环变量,以便闭包关闭变量,该变量永不更改。

for (int iterator=0; iterator < 10; iterator++)
{
    int i = iterator;
    listTask[iterator] = Task.Factory.StartNew(() =>
    {
        Int32 send = i + page * 10;
        DoStatus("Page: " + send.ToString());
        Processamento(parametros, filial, send);                        
    });
}

If I understand your question, you want to create 10 threads, wait for all, then recreate 10 threads, etc. Each thread load 100 results. 如果我理解您的问题,则要创建10个线程,等待所有线程,然后重新创建10个线程,依此类推。每个线程加载100个结果。

In this answer, results are String but that can be changed. 在此答案中,结果为String但可以更改。

private void Load()
{
    Boolean loading = true;
    List<String> listResult = new List<String>();
    Int32 boucle = 0;

    Task[] listTask = new Task[10];

    do
    {
        // create 10 threads (=1000 results)
        for (int iterator=0; iterator < 10; iterator++)
        {
            // [0-99] [100-199] [200-299] ...
            Int32 start = 100 * iterator + 1000 * boucle;
            Int32 end = start + 99;

            listTask[iterator] = Task<List<String>>.Factory.StartNew(() =>
            {
                List<String> data = LoadData(start, end);
                return data;
            });
        }

        // wait for 10 threads to finish
        Task.WaitAll(listTask);

        // collapse results
        for (int i=0; i < 10; i++)
        {
            listResult.AddRange((listTask[i] as Task<List<String>>).Result);
        }

        // check if there is 100 results in last thread
        loading = (listTask[9] as Task<List<String>>).Result.Count == 100;

        // ready for another iteration (next 1000 results)
        boucle++;
    }
    while (loading);
}

private List<string> LoadData(int p1, int p2)
{
    // TODO : load data from p1 to p2
    throw new NotImplementedException();
}

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

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