代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace Utils{ ////// 线程工具类 /// public class ThreadUtil { ////// 使用的逻辑处理器数 /// private static int _ProcessorCount; private static Semaphore _semaphore; private static List_TaskList = new List (); private static object _lock = new object(); static ThreadUtil() { _ProcessorCount = Environment.ProcessorCount * 2 / 4; //使用的逻辑处理器数 if (_ProcessorCount < 1) _ProcessorCount = 1; _semaphore = new Semaphore(_ProcessorCount, _ProcessorCount); } public static void Run(Action
使用方法:
ThreadUtil.Run((obj) =>{ //todo},arg,(ex)=>{ //错误处理});