c# 异步任务队列(可选是否使用单线程执行任务,以及自动取消任务)

使用demo,(.net framework 4.0 自行添加async wait 扩展库)

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("主线程"+Thread.CurrentThread.ManagedThreadId);
            var asyncTaskQueue = new AsyncTaskQueue
            {
                AutoCancelPreviousTask = true, // 自动取消之前的任务
                UseSingleThread = true // 使用单线程执行任务
            };

            // 快速启动20个任务
            for (var i = 1; i < 20; i++)
            {
                Test(asyncTaskQueue, i);
            }
            Console.WriteLine("运行结束");
            Console.ReadKey();
        }

        public static async void Test(AsyncTaskQueue taskQueue, int num)
        {
            var result = await taskQueue.Run(() =>
            {
                // 长时间耗时任务
                Thread.Sleep(5000);
                Console.WriteLine("输入的是" + num);
                return num * 100;
            });
            Console.WriteLine("当前线程" + Thread.CurrentThread.ManagedThreadId + "输出的的" + result);
        }
    }

这里是实现代码

#region summary

//   ------------------------------------------------------------------------------------------------
//   <copyright file="AsyncTaskQueue.cs" >
//     作者:mokeyish
//   </copyright>
//   ------------------------------------------------------------------------------------------------

#endregion

using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Security.Permissions;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace Test
{
    /// <summary>
    /// 异步任务队列
    /// </summary>
    public class AsyncTaskQueue : IDisposable
    {
        private bool _isDisposed;
        private readonly ConcurrentQueue<AwaitableTask> _queue = new ConcurrentQueue<AwaitableTask>();
        private Thread _thread;
        private AutoResetEvent _autoResetEvent;

        /// <summary>
        /// 异步任务队列
        /// </summary>
        public AsyncTaskQueue()
        {
            _autoResetEvent = new AutoResetEvent(false);
            _thread = new Thread(InternalRuning) {IsBackground = true};
            _thread.Start();
        }

        private bool TryGetNextTask(out AwaitableTask task)
        {
            task = null;
            while (_queue.Count > 0)
            {
                if (_queue.TryDequeue(out task) && (!AutoCancelPreviousTask || _queue.Count == 0)) return true;
                task.Cancel();
            }
            return false;
        }

        private AwaitableTask PenddingTask(AwaitableTask task)
        {
            lock (_queue)
            {
                Debug.Assert(task != null);
                _queue.Enqueue(task);
                _autoResetEvent.Set();
            }
            return task;
        }

        private void InternalRuning()
        {
            while (!_isDisposed)
            {
                if (_queue.Count == 0)
                {
                    _autoResetEvent.WaitOne();
                }
                while (TryGetNextTask(out var task))
                {
                    if (task.IsCancel) continue;

                    if (UseSingleThread)
                    {
                        task.RunSynchronously();
                    }
                    else
                    {
                        task.Start();
                    }
                }
            }
        }

        /// <summary>
        /// 是否使用单线程完成任务.
        /// </summary>
        public bool UseSingleThread { get; set; } = true;

        /// <summary>
        /// 自动取消以前的任务。
        /// </summary>
        public bool AutoCancelPreviousTask { get; set; } = false;

        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public AwaitableTask Run(Action action)
            => PenddingTask(new AwaitableTask(new Task(action, new CancellationToken(false))));

        /// <summary>
        /// 执行任务
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="function"></param>
        /// <returns></returns>
        public AwaitableTask<TResult> Run<TResult>(Func<TResult> function)
            => (AwaitableTask<TResult>) PenddingTask(new AwaitableTask<TResult>(new Task<TResult>(function)));


        /// <inheritdoc />
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        
        /// <summary>
        /// 析构任务队列
        /// </summary>
        ~AsyncTaskQueue() => Dispose(false);

        private void Dispose(bool disposing)
        {
            if (_isDisposed) return;
            if (disposing)
            {
                _autoResetEvent.Dispose();
            }
            _thread = null;
            _autoResetEvent = null;
            _isDisposed = true;
        }

        /// <summary>
        /// 可等待的任务
        /// </summary>
        public class AwaitableTask
        {
            private readonly Task _task;

            /// <summary>
            /// 初始化可等待的任务。
            /// </summary>
            /// <param name="task"></param>
            public AwaitableTask(Task task) => _task = task;

            /// <summary>
            /// 任务的Id
            /// </summary>
            public int TaskId => _task.Id;

            /// <summary>
            /// 任务是否取消
            /// </summary>
            public bool IsCancel { get; private set; }

            /// <summary>
            /// 开始任务
            /// </summary>
            public void Start() => _task.Start();

            /// <summary>
            /// 同步执行开始任务
            /// </summary>
            public void RunSynchronously() => _task.RunSynchronously();

            /// <summary>
            /// 取消任务
            /// </summary>
            public void Cancel() => IsCancel = true;

            /// <summary>
            /// 获取任务等待器
            /// </summary>
            /// <returns></returns>
            public TaskAwaiter GetAwaiter() => new TaskAwaiter(this);

            /// <summary>Provides an object that waits for the completion of an asynchronous task. </summary>
            [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true, Synchronization = true)]
            public struct TaskAwaiter : INotifyCompletion
            {
                private readonly AwaitableTask _task;

                /// <summary>
                /// 任务等待器
                /// </summary>
                /// <param name="awaitableTask"></param>
                public TaskAwaiter(AwaitableTask awaitableTask) => _task = awaitableTask;

                /// <summary>
                /// 任务是否完成.
                /// </summary>
                public bool IsCompleted => _task._task.IsCompleted;

                /// <inheritdoc />
                public void OnCompleted(Action continuation)
                {
                    var This = this;
                    _task._task.ContinueWith(t =>
                    {
                        if (!This._task.IsCancel) continuation?.Invoke();
                    });
                }
                /// <summary>
                /// 获取任务结果
                /// </summary>
                public void GetResult() => _task._task.Wait();
            }
        }

        /// <summary>
        /// 可等待的任务
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        public class AwaitableTask<TResult> : AwaitableTask
        {
            /// <summary>
            /// 初始化可等待的任务
            /// </summary>
            /// <param name="task">需要执行的任务</param>
            public AwaitableTask(Task<TResult> task) : base(task) => _task = task;


            private readonly Task<TResult> _task;

            /// <summary>
            /// 获取任务等待器
            /// </summary>
            /// <returns></returns>
            public new TaskAwaiter GetAwaiter() => new TaskAwaiter(this);

            /// <summary>
            /// 任务等待器
            /// </summary>
            [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true, Synchronization = true)]
            public new struct TaskAwaiter : INotifyCompletion
            {
                private readonly AwaitableTask<TResult> _task;

                /// <summary>
                /// 初始化任务等待器
                /// </summary>
                /// <param name="awaitableTask"></param>
                public TaskAwaiter(AwaitableTask<TResult> awaitableTask) => _task = awaitableTask;

                /// <summary>
                /// 任务是否已完成。
                /// </summary>
                public bool IsCompleted => _task._task.IsCompleted;

                /// <inheritdoc />
                public void OnCompleted(Action continuation)
                {
                    var This = this;
                    _task._task.ContinueWith(t =>
                    {
                        if (!This._task.IsCancel) continuation?.Invoke();
                    });
                }

                /// <summary>
                /// 获取任务结果。
                /// </summary>
                /// <returns></returns>
                public TResult GetResult() => _task._task.Result;
            }
        }
    }
}

原文地址:https://www.cnblogs.com/mokeyish/p/6986560.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值