├── .gitignore ├── LICENSE ├── README.md ├── TaskSchedulerEx.Test ├── App.config ├── Form1.Designer.cs ├── Form1.cs ├── Form1.resx ├── Program.cs ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings └── TaskSchedulerEx.Test.csproj ├── TaskSchedulerEx.sln └── TaskSchedulerEx ├── RunHelper.cs ├── TaskSchedulerEx.cs ├── TaskSchedulerEx.csproj └── TaskSchedulerEx.nuspec /.gitignore: -------------------------------------------------------------------------------- 1 | bin 2 | obj 3 | packages 4 | .vs 5 | lib 6 | 7 | *.suo 8 | *.user 9 | 10 | *.nupkg 11 | 12 | nuget.exe 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 0611163 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TaskSchedulerEx 2 | 一个自定义的C# TaskScheduler,独立线程池 3 | 4 | ## 特点 5 | 6 | 1. 使用独立线程池,线程池中线程分为核心线程和辅助线程,辅助线程会动态增加和释放,且总线程数不大于参数_maxThreadCount 7 | 8 | 2. 无缝兼容Task,使用上和Task一样,可以用它来实现异步,参见:C# async await 异步执行方法封装 替代 BackgroundWorker 9 | 10 | 3. 队列中尚未执行的任务可以取消 11 | 12 | 4. 通过扩展类TaskHelper实现任务分组 13 | 14 | 5. 和SmartThreadPool对比,优点是无缝兼容Task类,和Task类使用没有区别,因为它本身就是对Task、TaskScheduler的扩展,所以Task类的ContinueWith、WaitAll等方法它都支持,以及兼容async、await异步编程 15 | 16 | 6. 代码量相当精简,TaskSchedulerEx类只有260多行代码 17 | 18 | 7. 池中的线程数量会根据负载自动增减,支持,但没有SmartThreadPool智能,为了性能,使用了比较笨的方式实现,不知道大家有没有既智能,性能又高的方案,我有一个思路,在定时器中计算每个任务执行平均耗时,然后使用公式(线程数 = CPU核心数 * ( 本地计算时间 + 等待时间 ) / 本地计算时间)来计算最佳线程数,然后按最佳线程数来动态创建线程,但这个计算过程可能会牺牲性能 19 | 20 | ## 使用示例 21 | 22 | ```C# 23 | using System; 24 | using System.Windows.Forms; 25 | using Utils; 26 | 27 | /** 28 | * TaskSchedulerEx 使用示例 29 | */ 30 | 31 | namespace TaskSchedulerExTest 32 | { 33 | public partial class Form1 : Form 34 | { 35 | private TaskSchedulerEx _taskEx = new TaskSchedulerEx(20, 20); 36 | 37 | public Form1() 38 | { 39 | InitializeComponent(); 40 | } 41 | 42 | private void Form1_FormClosing(object sender, FormClosingEventArgs e) 43 | { 44 | if (_taskEx != null) 45 | { 46 | _taskEx.Dispose(); //释放资源 47 | } 48 | } 49 | 50 | #region Log 51 | private void Log(string log) 52 | { 53 | if (!this.IsDisposed) 54 | { 55 | if (this.InvokeRequired) 56 | { 57 | this.BeginInvoke(new Action(() => 58 | { 59 | textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + " " + log + "\r\n\r\n"); 60 | })); 61 | } 62 | else 63 | { 64 | textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + " " + log + "\r\n\r\n"); 65 | } 66 | } 67 | } 68 | #endregion 69 | 70 | //基本用法 71 | private void button1_Click(object sender, EventArgs e) 72 | { 73 | _taskEx.Run(() => 74 | { 75 | Log("简单测试"); 76 | }); 77 | } 78 | 79 | //传递参数 80 | private void button2_Click(object sender, EventArgs e) 81 | { 82 | _taskEx.Run((obj) => 83 | { 84 | Log("输入的参数是:" + obj ?? obj.ToString()); 85 | }, "参数1"); 86 | } 87 | 88 | //异步用法 89 | private async void button3_Click(object sender, EventArgs e) 90 | { 91 | for (int i = 1; i <= 10; i++) 92 | { 93 | await _taskEx.Run((obj) => 94 | { 95 | int k = (int)obj; 96 | Log("异步测试,i=" + k); 97 | }, i); 98 | } 99 | } 100 | 101 | //返回值测试 102 | private async void button4_Click(object sender, EventArgs e) 103 | { 104 | string result = await _taskEx.Run(() => 105 | { 106 | return "返回值测试"; 107 | }); 108 | Log(result); 109 | } 110 | } 111 | } 112 | ``` 113 | -------------------------------------------------------------------------------- /TaskSchedulerEx.Test/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /TaskSchedulerEx.Test/Form1.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace TaskSchedulerExTest 2 | { 3 | partial class Form1 4 | { 5 | /// 6 | /// 必需的设计器变量。 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// 清理所有正在使用的资源。 12 | /// 13 | /// 如果应释放托管资源,为 true;否则为 false。 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows 窗体设计器生成的代码 24 | 25 | /// 26 | /// 设计器支持所需的方法 - 不要修改 27 | /// 使用代码编辑器修改此方法的内容。 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.textBox1 = new System.Windows.Forms.TextBox(); 32 | this.button1 = new System.Windows.Forms.Button(); 33 | this.button2 = new System.Windows.Forms.Button(); 34 | this.button3 = new System.Windows.Forms.Button(); 35 | this.button4 = new System.Windows.Forms.Button(); 36 | this.button5 = new System.Windows.Forms.Button(); 37 | this.SuspendLayout(); 38 | // 39 | // textBox1 40 | // 41 | this.textBox1.Location = new System.Drawing.Point(93, 12); 42 | this.textBox1.Multiline = true; 43 | this.textBox1.Name = "textBox1"; 44 | this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; 45 | this.textBox1.Size = new System.Drawing.Size(654, 540); 46 | this.textBox1.TabIndex = 4; 47 | // 48 | // button1 49 | // 50 | this.button1.Location = new System.Drawing.Point(12, 12); 51 | this.button1.Name = "button1"; 52 | this.button1.Size = new System.Drawing.Size(75, 23); 53 | this.button1.TabIndex = 5; 54 | this.button1.Text = "button1"; 55 | this.button1.UseVisualStyleBackColor = true; 56 | this.button1.Click += new System.EventHandler(this.button1_Click); 57 | // 58 | // button2 59 | // 60 | this.button2.Location = new System.Drawing.Point(12, 57); 61 | this.button2.Name = "button2"; 62 | this.button2.Size = new System.Drawing.Size(75, 23); 63 | this.button2.TabIndex = 6; 64 | this.button2.Text = "button2"; 65 | this.button2.UseVisualStyleBackColor = true; 66 | this.button2.Click += new System.EventHandler(this.button2_Click); 67 | // 68 | // button3 69 | // 70 | this.button3.Location = new System.Drawing.Point(12, 105); 71 | this.button3.Name = "button3"; 72 | this.button3.Size = new System.Drawing.Size(75, 23); 73 | this.button3.TabIndex = 7; 74 | this.button3.Text = "button3"; 75 | this.button3.UseVisualStyleBackColor = true; 76 | this.button3.Click += new System.EventHandler(this.button3_Click); 77 | // 78 | // button4 79 | // 80 | this.button4.Location = new System.Drawing.Point(12, 149); 81 | this.button4.Name = "button4"; 82 | this.button4.Size = new System.Drawing.Size(75, 23); 83 | this.button4.TabIndex = 8; 84 | this.button4.Text = "button4"; 85 | this.button4.UseVisualStyleBackColor = true; 86 | this.button4.Click += new System.EventHandler(this.button4_Click); 87 | // 88 | // button5 89 | // 90 | this.button5.Location = new System.Drawing.Point(12, 198); 91 | this.button5.Name = "button5"; 92 | this.button5.Size = new System.Drawing.Size(75, 23); 93 | this.button5.TabIndex = 9; 94 | this.button5.Text = "button5"; 95 | this.button5.UseVisualStyleBackColor = true; 96 | this.button5.Click += new System.EventHandler(this.button5_Click); 97 | // 98 | // Form1 99 | // 100 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 101 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 102 | this.ClientSize = new System.Drawing.Size(767, 573); 103 | this.Controls.Add(this.button5); 104 | this.Controls.Add(this.button4); 105 | this.Controls.Add(this.button3); 106 | this.Controls.Add(this.button2); 107 | this.Controls.Add(this.button1); 108 | this.Controls.Add(this.textBox1); 109 | this.MaximizeBox = false; 110 | this.Name = "Form1"; 111 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 112 | this.Text = "TaskSchedulerEx测试"; 113 | this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); 114 | this.ResumeLayout(false); 115 | this.PerformLayout(); 116 | 117 | } 118 | 119 | #endregion 120 | 121 | private System.Windows.Forms.TextBox textBox1; 122 | private System.Windows.Forms.Button button1; 123 | private System.Windows.Forms.Button button2; 124 | private System.Windows.Forms.Button button3; 125 | private System.Windows.Forms.Button button4; 126 | private System.Windows.Forms.Button button5; 127 | } 128 | } 129 | 130 | -------------------------------------------------------------------------------- /TaskSchedulerEx.Test/Form1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Data; 5 | using System.Diagnostics; 6 | using System.Drawing; 7 | using System.Linq; 8 | using System.Text; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | using System.Windows.Forms; 12 | using Utils; 13 | 14 | namespace TaskSchedulerExTest 15 | { 16 | public partial class Form1 : Form 17 | { 18 | private TaskSchedulerEx _taskEx = new TaskSchedulerEx(0, 20); 19 | 20 | public Form1() 21 | { 22 | InitializeComponent(); 23 | ThreadPool.SetMinThreads(20, 20); 24 | } 25 | 26 | private void Form1_FormClosing(object sender, FormClosingEventArgs e) 27 | { 28 | if (_taskEx != null) 29 | { 30 | _taskEx.Dispose(); //释放资源 31 | } 32 | } 33 | 34 | #region Log 35 | private void Log(string log) 36 | { 37 | if (!this.IsDisposed) 38 | { 39 | if (this.InvokeRequired) 40 | { 41 | this.BeginInvoke(new Action(() => 42 | { 43 | textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + " " + log + "\r\n\r\n"); 44 | })); 45 | } 46 | else 47 | { 48 | textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + " " + log + "\r\n\r\n"); 49 | } 50 | } 51 | } 52 | #endregion 53 | 54 | private void button1_Click(object sender, EventArgs e) 55 | { 56 | _taskEx.Run(() => 57 | { 58 | Log("简单测试"); 59 | }); 60 | } 61 | 62 | private void button2_Click(object sender, EventArgs e) 63 | { 64 | Log("==== 开始 ========"); 65 | Stopwatch stopwatch = new Stopwatch(); 66 | stopwatch.Start(); 67 | 68 | _taskEx.Run(() => 69 | { 70 | int n = 100; 71 | int count = 0; 72 | List taskList = new List(); 73 | for (int i = 0; i < n; i++) 74 | { 75 | Task task = _taskEx.Run((obj) => 76 | { 77 | Interlocked.Increment(ref count); 78 | int k = (int)obj; 79 | Thread.Sleep(100); //模拟耗时 80 | Log("测试 " + k.ToString("000")); 81 | }, i); 82 | taskList.Add(task); 83 | } 84 | 85 | Task.WaitAll(taskList.ToArray()); 86 | 87 | Log($"==== 结束,count={count},耗时:{stopwatch.Elapsed.TotalSeconds.ToString("0.000")} 秒 ========"); 88 | stopwatch.Stop(); 89 | }); 90 | } 91 | 92 | private async void button3_Click(object sender, EventArgs e) 93 | { 94 | Log("==== 开始 ========"); 95 | Stopwatch stopwatch = new Stopwatch(); 96 | stopwatch.Start(); 97 | 98 | int n = 100; 99 | int count = 0; 100 | List taskList = new List(); 101 | for (int i = 0; i < n; i++) 102 | { 103 | Task task = _taskEx.Run((obj) => 104 | { 105 | Interlocked.Increment(ref count); 106 | int k = (int)obj; 107 | Thread.Sleep(100); //模拟耗时 108 | Log("测试 " + k.ToString("000")); 109 | }, i); 110 | taskList.Add(task); 111 | } 112 | 113 | foreach (Task tsk in taskList) 114 | { 115 | await tsk; 116 | } 117 | 118 | Log($"==== 结束,count={count},耗时:{stopwatch.Elapsed.TotalSeconds.ToString("0.000")} 秒 ========"); 119 | stopwatch.Stop(); 120 | } 121 | 122 | //使用C#原生Task类测试 123 | private void button4_Click(object sender, EventArgs e) 124 | { 125 | Log("==== 开始 ========"); 126 | Stopwatch stopwatch = new Stopwatch(); 127 | stopwatch.Start(); 128 | 129 | Task.Run(() => 130 | { 131 | int n = 100; 132 | int count = 0; 133 | List taskList = new List(); 134 | for (int i = 0; i < n; i++) 135 | { 136 | Task task = Task.Factory.StartNew((obj) => 137 | { 138 | Interlocked.Increment(ref count); 139 | int k = (int)obj; 140 | Thread.Sleep(100); //模拟耗时 141 | Log("测试 " + k.ToString("000")); 142 | }, i); 143 | taskList.Add(task); 144 | } 145 | 146 | Task.WaitAll(taskList.ToArray()); 147 | 148 | Log($"==== 结束,count={count},耗时:{stopwatch.Elapsed.TotalSeconds.ToString("0.000")} 秒 ========"); 149 | stopwatch.Stop(); 150 | }); 151 | } 152 | 153 | private void button5_Click(object sender, EventArgs e) 154 | { 155 | Task.Run(() => 156 | { 157 | Task.Run(() => 158 | { 159 | Log("==== 开始 ========"); 160 | Stopwatch stopwatch = new Stopwatch(); 161 | stopwatch.Start(); 162 | 163 | int count = 0; 164 | List taskList = new List(); 165 | for (int i = 0; i < 1000; i++) 166 | { 167 | Task task = _taskEx.Run((obj) => 168 | { 169 | Interlocked.Increment(ref count); 170 | int k = (int)obj; 171 | if (k % 100 == 0) 172 | { 173 | Log("测试 " + k.ToString("000")); 174 | } 175 | }, i); 176 | taskList.Add(task); 177 | if (i % 10 == 0) 178 | { 179 | Thread.Sleep(1); 180 | } 181 | } 182 | 183 | Task.WaitAll(taskList.ToArray()); 184 | 185 | Log($"==== 结束,count={count},耗时:{stopwatch.Elapsed.TotalSeconds.ToString("0.000")} 秒 ========"); 186 | stopwatch.Stop(); 187 | }); 188 | 189 | Thread.Sleep(20100); 190 | 191 | Task.Run(() => 192 | { 193 | Log("==== 开始 ========"); 194 | Stopwatch stopwatch = new Stopwatch(); 195 | stopwatch.Start(); 196 | 197 | int count = 0; 198 | List taskList = new List(); 199 | for (int i = 0; i < 20; i++) 200 | { 201 | Task task = _taskEx.Run((obj) => 202 | { 203 | Interlocked.Increment(ref count); 204 | int k = (int)obj; 205 | Log("测试 " + k.ToString("000")); 206 | }, i); 207 | taskList.Add(task); 208 | Thread.Sleep(500); 209 | } 210 | 211 | Task.WaitAll(taskList.ToArray()); 212 | 213 | Log($"==== 结束,count={count},耗时:{stopwatch.Elapsed.TotalSeconds.ToString("0.000")} 秒 ========"); 214 | stopwatch.Stop(); 215 | }); 216 | }); 217 | } 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /TaskSchedulerEx.Test/Form1.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /TaskSchedulerEx.Test/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using System.Windows.Forms; 6 | 7 | namespace TaskSchedulerExTest.Test 8 | { 9 | static class Program 10 | { 11 | /// 12 | /// 应用程序的主入口点。 13 | /// 14 | [STAThread] 15 | static void Main() 16 | { 17 | Application.EnableVisualStyles(); 18 | Application.SetCompatibleTextRenderingDefault(false); 19 | Application.Run(new Form1()); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /TaskSchedulerEx.Test/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("TaskSchedulerEx.Test")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("TaskSchedulerEx.Test")] 13 | [assembly: AssemblyCopyright("Copyright © 2021")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | //将 ComVisible 设置为 false 将使此程序集中的类型 18 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("da912d04-5bc3-4c8b-bb6f-95d7fc6351f7")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: : 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /TaskSchedulerEx.Test/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本:4.0.30319.42000 5 | // 6 | // 对此文件的更改可能会导致不正确的行为,并且如果 7 | // 重新生成代码,这些更改将会丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace TaskSchedulerExTest.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// 一个强类型的资源类,用于查找本地化的字符串等。 17 | /// 18 | // 此类是由 StronglyTypedResourceBuilder 19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 20 | // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen 21 | // (以 /str 作为命令选项),或重新生成 VS 项目。 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// 返回此类使用的缓存的 ResourceManager 实例。 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TaskSchedulerExTest.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// 使用此强类型资源类,为所有资源查找 51 | /// 重写当前线程的 CurrentUICulture 属性。 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /TaskSchedulerEx.Test/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /TaskSchedulerEx.Test/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本:4.0.30319.42000 5 | // 6 | // 对此文件的更改可能会导致不正确的行为,并且如果 7 | // 重新生成代码,这些更改将会丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace TaskSchedulerExTest.Properties { 12 | 13 | 14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] 16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { 17 | 18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 19 | 20 | public static Settings Default { 21 | get { 22 | return defaultInstance; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /TaskSchedulerEx.Test/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /TaskSchedulerEx.Test/TaskSchedulerEx.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {DA912D04-5BC3-4C8B-BB6F-95D7FC6351F7} 8 | WinExe 9 | Properties 10 | TaskSchedulerExTest 11 | TaskSchedulerEx.Test 12 | v4.5.2 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | Form 51 | 52 | 53 | Form1.cs 54 | 55 | 56 | 57 | 58 | Form1.cs 59 | 60 | 61 | ResXFileCodeGenerator 62 | Resources.Designer.cs 63 | Designer 64 | 65 | 66 | True 67 | Resources.resx 68 | True 69 | 70 | 71 | SettingsSingleFileGenerator 72 | Settings.Designer.cs 73 | 74 | 75 | True 76 | Settings.settings 77 | True 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | {620577cc-005e-4cf0-9d99-7905acb62bea} 86 | TaskSchedulerEx 87 | 88 | 89 | 90 | 97 | -------------------------------------------------------------------------------- /TaskSchedulerEx.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.2.32602.215 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TaskSchedulerEx.Test", "TaskSchedulerEx.Test\TaskSchedulerEx.Test.csproj", "{DA912D04-5BC3-4C8B-BB6F-95D7FC6351F7}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TaskSchedulerEx", "TaskSchedulerEx\TaskSchedulerEx.csproj", "{620577CC-005E-4CF0-9D99-7905ACB62BEA}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {DA912D04-5BC3-4C8B-BB6F-95D7FC6351F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {DA912D04-5BC3-4C8B-BB6F-95D7FC6351F7}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {DA912D04-5BC3-4C8B-BB6F-95D7FC6351F7}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {DA912D04-5BC3-4C8B-BB6F-95D7FC6351F7}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {620577CC-005E-4CF0-9D99-7905ACB62BEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {620577CC-005E-4CF0-9D99-7905ACB62BEA}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {620577CC-005E-4CF0-9D99-7905ACB62BEA}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {620577CC-005E-4CF0-9D99-7905ACB62BEA}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {707374B1-1D4B-4249-AD0C-C501F60AF7FC} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /TaskSchedulerEx/RunHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | 8 | namespace Utils 9 | { 10 | /// 11 | /// 线程工具类 12 | /// 13 | public static class RunHelper 14 | { 15 | #region 变量属性事件 16 | 17 | #endregion 18 | 19 | #region 线程中执行 20 | /// 21 | /// 线程中执行 22 | /// 23 | public static Task Run(this TaskScheduler scheduler, Action doWork, object arg = null, Action errorAction = null) 24 | { 25 | return Task.Factory.StartNew((obj) => 26 | { 27 | try 28 | { 29 | doWork(obj); 30 | } 31 | catch (Exception ex) 32 | { 33 | if (errorAction != null) errorAction(ex); 34 | LogUtil.Error(ex, "RunHelper.Run错误"); 35 | } 36 | }, arg, CancellationToken.None, TaskCreationOptions.None, scheduler); 37 | } 38 | #endregion 39 | 40 | #region 线程中执行 41 | /// 42 | /// 线程中执行 43 | /// 44 | public static Task Run(this TaskScheduler scheduler, Action doWork, Action errorAction = null) 45 | { 46 | return Task.Factory.StartNew(() => 47 | { 48 | try 49 | { 50 | doWork(); 51 | } 52 | catch (Exception ex) 53 | { 54 | if (errorAction != null) errorAction(ex); 55 | LogUtil.Error(ex, "RunHelper.Run错误"); 56 | } 57 | }, CancellationToken.None, TaskCreationOptions.None, scheduler); 58 | } 59 | #endregion 60 | 61 | #region 线程中执行 62 | /// 63 | /// 线程中执行 64 | /// 65 | public static Task Run(this TaskScheduler scheduler, Func doWork, object arg = null, Action errorAction = null) 66 | { 67 | return Task.Factory.StartNew((obj) => 68 | { 69 | try 70 | { 71 | return doWork(obj); 72 | } 73 | catch (Exception ex) 74 | { 75 | if (errorAction != null) errorAction(ex); 76 | LogUtil.Error(ex, "RunHelper.Run错误"); 77 | return default(T); 78 | } 79 | }, arg, CancellationToken.None, TaskCreationOptions.None, scheduler); 80 | } 81 | #endregion 82 | 83 | #region 线程中执行 84 | /// 85 | /// 线程中执行 86 | /// 87 | public static Task Run(this TaskScheduler scheduler, Func doWork, Action errorAction = null) 88 | { 89 | return Task.Factory.StartNew(() => 90 | { 91 | try 92 | { 93 | return doWork(); 94 | } 95 | catch (Exception ex) 96 | { 97 | if (errorAction != null) errorAction(ex); 98 | LogUtil.Error(ex, "RunHelper.Run错误"); 99 | return default(T); 100 | } 101 | }, CancellationToken.None, TaskCreationOptions.None, scheduler); 102 | } 103 | #endregion 104 | 105 | #region 线程中执行 106 | /// 107 | /// 线程中执行 108 | /// 109 | public static async Task RunAsync(this TaskScheduler scheduler, Func doWork, object arg = null, Action errorAction = null) 110 | { 111 | return await Task.Factory.StartNew((obj) => 112 | { 113 | try 114 | { 115 | return doWork(obj); 116 | } 117 | catch (Exception ex) 118 | { 119 | if (errorAction != null) errorAction(ex); 120 | LogUtil.Error(ex, "RunHelper.RunAsync错误"); 121 | return default(T); 122 | } 123 | }, arg, CancellationToken.None, TaskCreationOptions.None, scheduler); 124 | } 125 | #endregion 126 | 127 | #region 线程中执行 128 | /// 129 | /// 线程中执行 130 | /// 131 | public static async Task RunAsync(this TaskScheduler scheduler, Func doWork, Action errorAction = null) 132 | { 133 | return await Task.Factory.StartNew(() => 134 | { 135 | try 136 | { 137 | return doWork(); 138 | } 139 | catch (Exception ex) 140 | { 141 | if (errorAction != null) errorAction(ex); 142 | LogUtil.Error(ex, "RunHelper.RunAsync错误"); 143 | return default(T); 144 | } 145 | }, CancellationToken.None, TaskCreationOptions.None, scheduler); 146 | } 147 | #endregion 148 | 149 | #region 线程中执行 150 | /// 151 | /// 线程中执行 152 | /// 153 | public static async Task RunAsync(this TaskScheduler scheduler, Action doWork, object arg = null, Action errorAction = null) 154 | { 155 | await Task.Factory.StartNew((obj) => 156 | { 157 | try 158 | { 159 | doWork(obj); 160 | } 161 | catch (Exception ex) 162 | { 163 | if (errorAction != null) errorAction(ex); 164 | LogUtil.Error(ex, "RunHelper.RunAsync错误"); 165 | } 166 | }, arg, CancellationToken.None, TaskCreationOptions.None, scheduler); 167 | } 168 | #endregion 169 | 170 | #region 线程中执行 171 | /// 172 | /// 线程中执行 173 | /// 174 | public static async Task RunAsync(this TaskScheduler scheduler, Action doWork, Action errorAction = null) 175 | { 176 | await Task.Factory.StartNew(() => 177 | { 178 | try 179 | { 180 | doWork(); 181 | } 182 | catch (Exception ex) 183 | { 184 | if (errorAction != null) errorAction(ex); 185 | LogUtil.Error(ex, "RunHelper.RunAsync错误"); 186 | } 187 | }, CancellationToken.None, TaskCreationOptions.None, scheduler); 188 | } 189 | #endregion 190 | 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /TaskSchedulerEx/TaskSchedulerEx.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Runtime.InteropServices; 6 | using System.Text; 7 | using System.Threading; 8 | using System.Threading.Tasks; 9 | 10 | namespace Utils 11 | { 12 | /// 13 | /// TaskScheduler扩展 14 | /// 每个实例都是独立线程池 15 | /// 16 | public class TaskSchedulerEx : TaskScheduler, IDisposable 17 | { 18 | #region 变量属性事件 19 | private ConcurrentQueue _tasks = new ConcurrentQueue(); 20 | private int _coreThreadCount = 0; 21 | private int _maxThreadCount = 0; 22 | private int _auxiliaryThreadTimeOut = 20000; //辅助线程释放时间 23 | private int _activeThreadCount = 0; 24 | private System.Timers.Timer _timer; 25 | private bool _run = true; 26 | private Semaphore _sem = null; 27 | private int _semMaxCount = int.MaxValue; //可以同时授予的信号量的最大请求数 28 | private int _semCount = 0; //可用信号量请求数 29 | private int _runCount = 0; //正在执行的和等待执行的任务数量 30 | 31 | /// 32 | /// 活跃线程数 33 | /// 34 | public int ActiveThreadCount 35 | { 36 | get { return _activeThreadCount; } 37 | } 38 | 39 | /// 40 | /// 核心线程数 41 | /// 42 | public int CoreThreadCount 43 | { 44 | get { return _coreThreadCount; } 45 | } 46 | 47 | /// 48 | /// 最大线程数 49 | /// 50 | public int MaxThreadCount 51 | { 52 | get { return _maxThreadCount; } 53 | } 54 | #endregion 55 | 56 | #region 构造函数 57 | /// 58 | /// TaskScheduler扩展 59 | /// 每个实例都是独立线程池 60 | /// 61 | /// 核心线程数(大于或等于0,不宜过大)(如果是一次性使用,则设置为0比较合适) 62 | /// 最大线程数 63 | public TaskSchedulerEx(int coreThreadCount = 10, int maxThreadCount = 20) 64 | { 65 | _sem = new Semaphore(0, _semMaxCount); 66 | _maxThreadCount = maxThreadCount; 67 | CreateCoreThreads(coreThreadCount); 68 | } 69 | #endregion 70 | 71 | #region override GetScheduledTasks 72 | protected override IEnumerable GetScheduledTasks() 73 | { 74 | return _tasks; 75 | } 76 | #endregion 77 | 78 | #region override TryExecuteTaskInline 79 | protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) 80 | { 81 | return false; 82 | } 83 | #endregion 84 | 85 | #region override QueueTask 86 | protected override void QueueTask(Task task) 87 | { 88 | _tasks.Enqueue(task); 89 | 90 | while (_semCount >= _semMaxCount) //信号量已满,等待 91 | { 92 | Thread.Sleep(1); 93 | } 94 | 95 | _sem.Release(); 96 | Interlocked.Increment(ref _semCount); 97 | 98 | Interlocked.Increment(ref _runCount); 99 | if (_activeThreadCount < _maxThreadCount && _activeThreadCount < _runCount) 100 | { 101 | CreateThread(); 102 | } 103 | } 104 | #endregion 105 | 106 | #region 资源释放 107 | /// 108 | /// 资源释放 109 | /// 队列中尚未执行的任务不再执行 110 | /// 111 | public void Dispose() 112 | { 113 | _run = false; 114 | 115 | if (_timer != null) 116 | { 117 | _timer.Stop(); 118 | _timer.Dispose(); 119 | _timer = null; 120 | } 121 | 122 | while (_activeThreadCount > 0) 123 | { 124 | _sem.Release(); 125 | Interlocked.Increment(ref _semCount); 126 | } 127 | 128 | _sem.Dispose(); 129 | } 130 | #endregion 131 | 132 | #region 创建核心线程池 133 | /// 134 | /// 创建核心线程池 135 | /// 136 | private void CreateCoreThreads(int? coreThreadCount = null) 137 | { 138 | if (coreThreadCount != null) _coreThreadCount = coreThreadCount.Value; 139 | 140 | for (int i = 0; i < _coreThreadCount; i++) 141 | { 142 | Interlocked.Increment(ref _activeThreadCount); 143 | Thread thread = null; 144 | thread = new Thread(new ThreadStart(() => 145 | { 146 | Task task; 147 | while (_run) 148 | { 149 | if (_tasks.TryDequeue(out task)) 150 | { 151 | TryExecuteTask(task); 152 | Interlocked.Decrement(ref _runCount); 153 | } 154 | else 155 | { 156 | _sem.WaitOne(); 157 | Interlocked.Decrement(ref _semCount); 158 | } 159 | } 160 | Interlocked.Decrement(ref _activeThreadCount); 161 | })); 162 | thread.IsBackground = true; 163 | thread.Start(); 164 | } 165 | } 166 | #endregion 167 | 168 | #region 创建辅助线程 169 | /// 170 | /// 创建辅助线程 171 | /// 172 | private void CreateThread() 173 | { 174 | Interlocked.Increment(ref _activeThreadCount); 175 | Thread thread = null; 176 | thread = new Thread(new ThreadStart(() => 177 | { 178 | Task task; 179 | while (_run) 180 | { 181 | if (_tasks.TryDequeue(out task)) 182 | { 183 | TryExecuteTask(task); 184 | Interlocked.Decrement(ref _runCount); 185 | } 186 | else 187 | { 188 | bool bl = _sem.WaitOne(_auxiliaryThreadTimeOut); 189 | if (!bl) break; 190 | Interlocked.Decrement(ref _semCount); 191 | } 192 | } 193 | Interlocked.Decrement(ref _activeThreadCount); 194 | })); 195 | thread.IsBackground = true; 196 | thread.Start(); 197 | } 198 | #endregion 199 | 200 | #region 全部取消 201 | /// 202 | /// 全部取消 203 | /// 取消队列中尚未执行的任务 204 | /// 205 | public void CancelAll() 206 | { 207 | Task tempTask; 208 | while (_tasks.TryDequeue(out tempTask)) 209 | { 210 | Interlocked.Decrement(ref _runCount); 211 | } 212 | } 213 | #endregion 214 | 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /TaskSchedulerEx/TaskSchedulerEx.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net45;netstandard2.0;net6.0 5 | disable 6 | disable 7 | 1.0.9 8 | 1.0.9 9 | 1.0.9 10 | True 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /TaskSchedulerEx/TaskSchedulerEx.nuspec: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | TaskSchedulerEx 5 | 1.0.9 6 | TaskSchedulerEx 7 | suxiang 8 | 9 | false 10 | MIT 11 | https://github.com/0611163/TaskSchedulerEx 12 | Task extension library 13 | 1. 优化:辅助线程可能会有概率不执行Task的问题 14 | 2. 升级LogUtil 15 | Copyright @ 2021 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | --------------------------------------------------------------------------------