在循环中的 C# 中使用 ThreadPool 并等待所有线程完成
Use ThreadPool in C# within a loop and wait for all threads to finish
我有一个在 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);
}
}
}
}
我希望 main
能够使用线程池和参数调用 Process
。然后我希望程序等待所有线程完成,然后再告诉用户它已完成。我该怎么做?似乎我不能在 Process 中添加参数,我得到:Error CS7036 There is no argument given that corresponds to the required formal parameter 'name' of 'Program.Process(object, int)'
而且我不清楚如何告诉 C# 等待循环中的所有进程完成,然后再告诉用户它已完成。
.NET快20岁了,里面有好几代改进的API。
这对于较新的 Task Parallel Library 方法来说是微不足道的。 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# 中看起来像这样的简单代码:
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);
}
}
}
}
我希望 main
能够使用线程池和参数调用 Process
。然后我希望程序等待所有线程完成,然后再告诉用户它已完成。我该怎么做?似乎我不能在 Process 中添加参数,我得到:Error CS7036 There is no argument given that corresponds to the required formal parameter 'name' of 'Program.Process(object, int)'
而且我不清楚如何告诉 C# 等待循环中的所有进程完成,然后再告诉用户它已完成。
.NET快20岁了,里面有好几代改进的API。
这对于较新的 Task Parallel Library 方法来说是微不足道的。 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);
}
}
}
}