5个构造函数(反编译得到,仅参考) 1/**////<summary> 2/// Constructs a Dispatcher instance using the default number of threads and no friendly tag 3/// 使用默认线程数和空名作为参数构造一个Dispatcher实例 4///</summary> 5public Dispatcher() 6 : this(0, null) 7{ 8} 9 10/**////<summary> 11/// Constructs a Dispatcher instance. 12/// The instance is usable only after AddPort is called at least once 13/// 该实例仅在调用AddPort之后才生效 14///</summary> 15///<param name="threadCount"> 16/// Number of OS threads to use for processing CCR Tasks 17/// 处理CCR任务的操作系统线程数 18///</param> 19///<param name="threadPoolName"> 20/// Friendly name to use for the OS Threads and this dispatcher instance 21/// Dispatcher实例和系统线程的名称 22///</param> 23public Dispatcher(int threadCount, string threadPoolName) 24 : this(threadCount, ThreadPriority.Normal, DispatcherOptions.None, threadPoolName) 25{ 26} 27 28/**////<summary> 29/// Constructs a Dispatcher instance. 30/// The instance is usable only after AddPort is called at least once 31///</summary> 32///<param name="threadCount"> 33/// Number of OS threads to use for processing CCR Tasks 34///</param> 35///<param name="priority"> 36/// OS Thread priority to use for threads exexuting CCR tasks 37/// 系统线程执行CCR任务所使用的优先级 38///</param> 39///<param name="options"> 40/// Dispatcher scheduling options 41/// 调度选项 42///</param> 43///<param name="threadPoolName"> 44/// Friendly name to use for the OS Threads and this dispatcher instance 45///</param> 46public Dispatcher(int threadCount, ThreadPriority priority, DispatcherOptions options, string threadPoolName) 47 : this(threadCount, priority, options, ApartmentState.Unknown, threadPoolName) 48{ 49} 50 51/**////<summary> 52/// Constructs a Dispatcher instance. 53/// The instance is usable only after AddPort is called at least once 54///</summary> 55///<param name="threadCount"> 56/// Number of OS threads to use for processing CCR Tasks 57///</param> 58///<param name="priority"> 59/// OS Thread priority to use for threads exexuting CCR tasks 60///</param> 61///<param name="useBackgroundThreads"> 62/// If true, background threads are used, which do not prevent application exit 63/// 是否使用后台线程,若是用,则应用程序可以自由的退出,而不用担心 64///</param> 65///<param name="threadPoolName"> 66/// Friendly name to use for the OS Threads and this dispatcher instance 67///</param> 68public Dispatcher(int threadCount, ThreadPriority priority, bool useBackgroundThreads, string threadPoolName) 69 : this(threadCount, 70 priority, 71 useBackgroundThreads ? DispatcherOptions.UseBackgroundThreads : DispatcherOptions.None, 72 threadPoolName) 73{ 74} 75 76/**////<summary> 77/// Constructs a Dispatcher instance. 78/// The instance is usable only after AddPort is called at least once 79///</summary> 80///<param name="threadCount"> 81/// Number of OS threads to use for processing CCR Tasks 82///</param> 83///<param name="priority"> 84/// OS Thread priority to use for threads exexuting CCR tasks 85///</param> 86///<param name="options"> 87/// Dispatcher scheduling options 88///</param> 89///<param name="threadApartmentState"> 90/// Thread apartment state. Use ApartmentState.Unknown when STA/MTA is not required for interop 91/// 线程单元状态,当COM interop不需要STA/MTA的时候可以使用ApartmentState.Unknown 92///</param> 93///<param name="threadPoolName"> 94/// Friendly name to use for the OS Threads and this dispatcher instance 95///</param> 96public Dispatcher(int threadCount, ThreadPriority priority, DispatcherOptions options, 97 ApartmentState threadApartmentState, string threadPoolName) 98{ 99this._startupCompleteEvent =new ManualResetEvent(false); 100this._dispatcherQueues =new List<DispatcherQueue>(); 101this._taskExecutionWorkers =new List<TaskExecutionWorker>(); 102this._nameToQueueTable =new Dictionary<string, DispatcherQueue>(); 103 104// 线程数 105if (threadCount ==0) 106{// 默认情况下 107 threadCount = Math.Max(NumberOfProcessorsInternal, 2) * ThreadsPerCpu; 108 } 109elseif (threadCount <0) 110{ 111thrownew ArgumentException("Cannot create a negative number of threads. Pass 0 to use default.", "threadCount"); 112 } 113 114if (threadPoolName ==null) 115{ 116this._name =string.Empty; 117 } 118else 119{ 120this._name = threadPoolName; 121 } 122 123this._options = options; 124 125for (int i =0; i < threadCount; i++) 126{ 127this.AddWorker(priority, threadApartmentState); 128 } 129 130this.StartWorkers(); 131} 132 133/**////<summary> 134/// Creates one TaskExecutionWorker instance associated with one OS thread 135/// 创建一个与线程想关联的TaskExecutionWorker实例 136///</summary> 137///<remarks> 138/// This routine should only be called once per dispatcher instance 139/// 每个Dispatcher实例只能调用该函数一次 140///</remarks> 141privatevoid AddWorker(ThreadPriority priority, ApartmentState apartmentState) 142{ 143 TaskExecutionWorker item =new TaskExecutionWorker(this); 144 145 Thread thread =new Thread(new ThreadStart(item.ExecutionLoop)); 146 thread.SetApartmentState(apartmentState); 147 thread.Name =this._name; 148 thread.Priority = priority; 149 thread.IsBackground = DispatcherOptions.None < (this._options & DispatcherOptions.UseBackgroundThreads); 150 151 item._thread = thread; 152 153this._taskExecutionWorkers.Add(item); 154this._cachedWorkerListCount++; 155}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端