简体   繁体   English

在循环中使用 C# 中的 ThreadPool 并等待所有线程完成

[英]Use ThreadPool in C# within a loop and wait for all threads to finish

I have a simple code that looks like this in C#:我在 C# 中有一个如下所示的简单代码:

using System;
using System.Threading;
using System.Diagnostics;


namespace ThreadPooling
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Enter the number of calculations to be made:");
            int calculations= Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Thread Pool Execution");

            for (int i = 1; i <= calculations; i++)
            {
                Console.WriteLine("Staring process " + i + "...");
                ThreadPool.QueueUserWorkItem(new WaitCallback(Process(i)));
            }

            Console.WriteLine("All calculations done.");
            Console.WriteLine("\nPress any key to exit the program...");
            Console.ReadKey();

        }

        static void Process(object callback, int name)
        {
            for (int i = 0; i <= 10; i++)
            {
                Console.WriteLine(i + " is the current number in " +  name);
            }
        }


    }
}

I want main to be able to call Process with Thread Pool and an argument.我希望main能够使用线程池和参数调用Process I then want the program to wait for all threads to finish before telling the user that it is done.然后我希望程序在告诉用户它完成之前等待所有线程完成。 How do I do both?我该怎么做? It seems I cannot put an argument inside Process, I get: Error CS7036 There is no argument given that corresponds to the required formal parameter 'name' of 'Program.Process(object, int)'似乎我无法在 Process 中放置参数,我得到: Error CS7036 There is no argument given that corresponds to the required formal parameter 'name' of 'Program.Process(object, int)'

And I am not clear on how to tell C# to wait for all processes with in the loop to finish before it tells the user it is done.而且我不清楚如何告诉 C# 在告诉用户它完成之前等待循环中的所有进程完成。

.NET is almost 20 years old, and has several generations of improving APIs in it. .NET 已经快 20 年了,里面有几代改进的 API。

This is trivial with the newer Task Parallel Library methods.这对于较新的任务并行库方法来说是微不足道的。 EG例如

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ThreadPooling
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Enter the number of calculations to be made:");
            int calculations = Convert.ToInt32(Console.ReadLine());

            var tasks = new List<Task>();
            for (int i = 1; i <= calculations; i++)
            {
                int processNum = i;
                Console.WriteLine("Staring process " + processNum + "...");
                var task = Task.Run(() => Process(processNum));
                tasks.Add(task);
            }

            Task.WaitAll(tasks.ToArray());
            Console.WriteLine("All calculations done.");
            Console.WriteLine("\nPress any key to exit the program...");
            Console.ReadKey();

        }

        static void Process(int name)
        {
            for (int i = 0; i <= 10; i++)
            {
                Console.WriteLine(i + " is the current number in " + name);
            }
        }


    }
}

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

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