TaskSchedulerEx 1.0.9

dotnet add package TaskSchedulerEx --version 1.0.9
                    
NuGet\Install-Package TaskSchedulerEx -Version 1.0.9
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TaskSchedulerEx" Version="1.0.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TaskSchedulerEx" Version="1.0.9" />
                    
Directory.Packages.props
<PackageReference Include="TaskSchedulerEx" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TaskSchedulerEx --version 1.0.9
                    
#r "nuget: TaskSchedulerEx, 1.0.9"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TaskSchedulerEx@1.0.9
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TaskSchedulerEx&version=1.0.9
                    
Install as a Cake Addin
#tool nuget:?package=TaskSchedulerEx&version=1.0.9
                    
Install as a Cake Tool

源码

https://github.com/0611163/TaskSchedulerEx

更新说明

DLL文件版本号问题。

如何使用

  1. 创建线程池
TaskSchedulerEx taskEx = new TaskSchedulerEx(20, 50); //创建线程池,其中20个核心线程,30个辅助线程,共50个线程
  1. 使用线程池
taskEx.Run(() =>
{
    Console.WriteLine("test");
});
Task.Factory.StartNew(() =>
{
    Console.WriteLine("test");
}, CancellationToken.None, TaskCreationOptions.None, taskEx);
  1. 释放线程池资源
taskEx.Dispose();

使用示例(Use example)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils;

namespace TaskSchedulerExTest
{
    public partial class Form1 : Form
    {
        private TaskSchedulerEx _taskEx = new TaskSchedulerEx(20, 20);

        public Form1()
        {
            InitializeComponent();
            ThreadPool.SetMinThreads(20, 20);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_taskEx != null)
            {
                _taskEx.Dispose(); //释放资源
            }
        }

        #region Log
        private void Log(string log)
        {
            if (!this.IsDisposed)
            {
                if (this.InvokeRequired)
                {
                    this.BeginInvoke(new Action(() =>
                    {
                        textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + " " + log + "\r\n\r\n");
                    }));
                }
                else
                {
                    textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + " " + log + "\r\n\r\n");
                }
            }
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            _taskEx.Run(() =>
            {
                Log("简单测试");
            });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Log("==== 开始 ========");
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            _taskEx.Run(() =>
            {
                int n = 100;
                List<Task> taskList = new List<Task>();
                for (int i = 0; i < n; i++)
                {
                    Task task = _taskEx.Run((obj) =>
                    {
                        int k = (int)obj;
                        Thread.Sleep(100); //模拟耗时
                        Log("测试 " + k.ToString("000"));
                    }, i);
                    taskList.Add(task);
                }

                Task.WaitAll(taskList.ToArray());

                Log("==== 结束 " + ",耗时:" + stopwatch.Elapsed.TotalSeconds.ToString("0.000") + " 秒 ========");
                stopwatch.Stop();
            });
        }

        private async void button3_Click(object sender, EventArgs e)
        {
            Log("==== 开始 ========");
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            int n = 100;
            List<Task> taskList = new List<Task>();
            for (int i = 0; i < n; i++)
            {
                Task task = _taskEx.Run((obj) =>
                {
                    int k = (int)obj;
                    Thread.Sleep(100); //模拟耗时
                    Log("测试 " + k.ToString("000"));
                }, i);
                taskList.Add(task);
            }

            foreach (Task tsk in taskList)
            {
                await tsk;
            }

            Log("==== 结束 " + ",耗时:" + stopwatch.Elapsed.TotalSeconds.ToString("0.000") + " 秒 ========");
            stopwatch.Stop();
        }

        //使用C#原生Task类测试
        private void button4_Click(object sender, EventArgs e)
        {
            Log("==== 开始 ========");
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            Task.Run(() =>
            {
                int n = 100;
                List<Task> taskList = new List<Task>();
                for (int i = 0; i < n; i++)
                {
                    Task task = Task.Factory.StartNew((obj) =>
                    {
                        int k = (int)obj;
                        Thread.Sleep(100); //模拟耗时
                        Log("测试 " + k.ToString("000"));
                    }, i);
                    taskList.Add(task);
                }

                Task.WaitAll(taskList.ToArray());

                Log("==== 结束 " + ",耗时:" + stopwatch.Elapsed.TotalSeconds.ToString("0.000") + " 秒 ========");
                stopwatch.Stop();
            });
        }
    }
}

说明

TaskSchedulerExtension TaskScheduler扩展

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.9 550 12/16/2022

1. 优化:辅助线程可能会有概率不执行Task的问题
2. 升级LogUtil