PS:前段时间稍微有些累,故暂停了本系列文章,今天状态开始恢复,继续旅程。
之前几章已经整体把CCR介绍了一下,为了更好地了解和使用CCR,接下来几章我将逐一介绍CCR内重要的几个类,本章要介绍的是CCR的心脏Dispatcher。
在我们使用Dispatcher的时候,最常用到的就是他的构造函数和资源释放函数了,因此本节,先介绍这2类函数,其他的函数因为涉及到DispatcherQueue等内部的运作机制,属于CCR内部的运作调度机制,留待前面这些基础知识交代完毕后再另起一篇细说。
一、构造函数
Dispatcher给我们提供了5个构造函数,其实是为了方便大家使用而对一个构造函数做的封装,我们看看具体的实现代码(我在关键的地方都加上了中文的注释):
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{
99 this._startupCompleteEvent = new ManualResetEvent(false);
100 this._dispatcherQueues = new List<DispatcherQueue>();
101 this._taskExecutionWorkers = new List<TaskExecutionWorker>();
102 this._nameToQueueTable = new Dictionary<string, DispatcherQueue>();
103
104 // 线程数
105 if (threadCount == 0)
106 {// 默认情况下
107 threadCount = Math.Max(NumberOfProcessorsInternal, 2) * ThreadsPerCpu;
108 }
109 else if (threadCount < 0)
110 {
111 throw new ArgumentException("Cannot create a negative number of threads. Pass 0 to use default.", "threadCount");
112 }
113
114 if (threadPoolName == null)
115 {
116 this._name = string.Empty;
117 }
118 else
119 {
120 this._name = threadPoolName;
121 }
122
123 this._options = options;
124
125 for (int i = 0; i < threadCount; i++)
126 {
127 this.AddWorker(priority, threadApartmentState);
128 }
129
130 this.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>
141private void AddWorker(ThreadPriority priority, ApartmentState apartmentState)
142{
143 TaskExecutionWorker item = new T
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{
99 this._startupCompleteEvent = new ManualResetEvent(false);
100 this._dispatcherQueues = new List<DispatcherQueue>();
101 this._taskExecutionWorkers = new List<TaskExecutionWorker>();
102 this._nameToQueueTable = new Dictionary<string, DispatcherQueue>();
103
104 // 线程数
105 if (threadCount == 0)
106 {// 默认情况下
107 threadCount = Math.Max(NumberOfProcessorsInternal, 2) * ThreadsPerCpu;
108 }
109 else if (threadCount < 0)
110 {
111 throw new ArgumentException("Cannot create a negative number of threads. Pass 0 to use default.", "threadCount");
112 }
113
114 if (threadPoolName == null)
115 {
116 this._name = string.Empty;
117 }
118 else
119 {
120 this._name = threadPoolName;
121 }
122
123 this._options = options;
124
125 for (int i = 0; i < threadCount; i++)
126 {
127 this.AddWorker(priority, threadApartmentState);
128 }
129
130 this.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>
141private void AddWorker(ThreadPriority priority, ApartmentState apartmentState)
142{
143 TaskExecutionWorker item = new T
作者: carburant
- 该日志由 carburant 于11年前发表在综合分类下,最后更新于 2012年12月12日.
- 转载请注明: 我的CCR之旅(4):倾听CCR的心跳声–Dispatcher(基础篇) | 学步园 +复制链接
抱歉!评论已关闭.