在循环中使用 C# 中的 ThreadPool 并等待所有线程完成 [英] Use ThreadPool in C# within a loop and wait for all threads to finish

查看:123
本文介绍了在循环中使用 C# 中的 ThreadPool 并等待所有线程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 C# 中看起来像这样的简单代码:

使用系统;使用 System.Threading;使用 System.Diagnostics;命名空间线程池{课程计划{static void Main(string[] args){Console.WriteLine("请输入要进行的计算次数:");int 计算 = Convert.ToInt32(Console.ReadLine());Console.WriteLine("线程池执行");for (int i = 1; i <= 计算; i++){Console.WriteLine("凝视进程" + i + "...");ThreadPool.QueueUserWorkItem(new WaitCallback(Process(i)));}Console.WriteLine("所有计算完成.");Console.WriteLine("\n按任意键退出程序...");Console.ReadKey();}静态无效过程(对象回调,int名称){for (int i = 0; i <= 10; i++){Console.WriteLine(i + " 是 " + name 中的当前数字);}}}}

我希望 main 能够使用线程池和参数调用 Process.然后我希望程序在告诉用户它完成之前等待所有线程完成.我该怎么做?似乎我无法在 Process 中放入参数,我得到:Error CS7036 没有给出与Program.Process(object, int)"的所需形式参数name"相对应的参数>

而且我不清楚如何告诉 C# 在它告诉用户它完成之前等待循环中的所有进程完成.

解决方案

.NET 已经有将近 20 年的历史,其中包含几代改进的 API.

这对于较新的 任务并行库方法.EG

使用系统;使用 System.Collections.Generic;使用 System.Threading.Tasks;命名空间线程池{课程计划{static void Main(string[] args){Console.WriteLine("请输入要进行的计算次数:");int 计算 = Convert.ToInt32(Console.ReadLine());var tasks = new List();for (int i = 1; i <= 计算; i++){int processNum = i;Console.WriteLine("盯着进程" + processNum + "...");var task = Task.Run(() => Process(processNum));任务.添加(任务);}Task.WaitAll(tasks.ToArray());Console.WriteLine("所有计算完成.");Console.WriteLine("\n按任意键退出程序...");Console.ReadKey();}静态无效进程(int名称){for (int i = 0; i <= 10; i++){Console.WriteLine(i + " 是 " + name 中的当前数字);}}}}

I have a simple code that looks like this in 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. 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)'

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.

解决方案

.NET is almost 20 years old, and has several generations of improving APIs in it.

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);
            }
        }


    }
}

这篇关于在循环中使用 C# 中的 ThreadPool 并等待所有线程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆