You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.1 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CreateThreadPool
{
internal class CreateThreadPool
{
public static void Create()
{
// Min 50, if over create 10 per once
ThreadPool.SetMinThreads(50, 10);
ThreadPool.QueueUserWorkItem(Calc);
ThreadPool.QueueUserWorkItem(Calc, 10.0);
ThreadPool.QueueUserWorkItem(Calc, 20.0);
}
// BackgroundWorker: Wrapper of ThreadPool
public static void CreateBackgroundWorker()
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += Worker_DoWork;
worker.RunWorkerAsync("Hello");
}
private static void Worker_DoWork(object sender, DoWorkEventArgs e)
{
int i = 0;
while (i < 10)
{
Console.WriteLine($"{(e.Argument != null ? e.Argument.ToString() : "")} Running... {++i}");
Thread.Sleep(1000);
}
}
private static void Calc(object radius)
{
if (radius == null)
return;
double r = (double)radius;
double area = r * r * Math.PI;
Console.WriteLine($"r={r}, area={area}");
}
}
}