Skip to main content
 首页 » 编程设计

c#之顺序异步任务队列不会等到异步任务结束

2024年02月05日14kuangbin

我正在尝试按顺序排列异步任务,所以我为此创建了一个类:

 public class TaskSequentialQueue : IDisposable, ITaskSequentialQueue 
    { 
        public delegate void OnExeptionDelegate(Exception ex); 
 
        private readonly Queue<Task> m_queue = new Queue<Task>(); 
        private readonly Object m_lock = new Object(); 
        private readonly CancellationTokenSource m_CancelToken = new CancellationTokenSource(); 
        private readonly OnExeptionDelegate m_onExeptionDelegate = null; 
        private Task m_currentTask = null; 
        private bool m_isDisposed = false;       
 
        public TaskSequentialQueue(OnExeptionDelegate expDelegate = null) 
        { 
            m_onExeptionDelegate = expDelegate; 
        } 
 
        public void Dispose() 
        { 
            Dispose(true); 
            GC.SuppressFinalize(this); 
        } 
 
        protected virtual void Dispose(bool isDisposing) 
        { 
            if (m_isDisposed) 
            { 
                return; 
            } 
 
            if (isDisposing) 
            { 
                lock (m_lock) 
                { 
                    m_isDisposed = true; 
                    m_queue.Clear(); 
                } 
 
                m_CancelToken.Cancel(); 
                m_CancelToken.Dispose(); 
            } 
        } 
 
        public void EnqueueTask( Task task) 
        { 
            lock (m_lock) 
            { 
                if (m_isDisposed) 
                    throw new ObjectDisposedException("TaskSequentialQueue"); 
 
                m_queue.Enqueue(task); 
            } 
 
            StartNextTask(); 
        } 
 
        public void EnqueueTask( Func<Task> task) 
        { 
            EnqueueTask(new Task<Task>(task)); 
        } 
 
        public Task EnqueueTaskAndWait( Task task) 
        { 
            TaskCompletionSource<int> taskSource = new TaskCompletionSource<int>(); 
            lock (m_lock) 
            { 
                if (m_isDisposed) 
                    throw new ObjectDisposedException("TaskSequentialQueue"); 
 
                Func<Task> toDo = async () => 
                {                                         
                    var waitabletask = task.ContinueWith( antecedent =>  
                                                            {  
                                                                taskSource.SetResult(0);  
                                                                if (antecedent.Exception != null) 
                                                                    throw antecedent.Exception;  
                                                            }); 
                    task.Start(); 
                    await waitabletask; 
                }; 
                this.EnqueueTask(toDo); 
            } 
 
            StartNextTask(); 
 
            return taskSource.Task;  //TODO! propagate the exception correctly ? 
        } 
 
        private void StartNextTask() 
        { 
            Task theTask = null; 
            lock(m_lock) 
            { 
                if (m_currentTask == null && m_queue.Count > 0 && !m_isDisposed) 
                { 
                    m_currentTask = m_queue.Dequeue(); 
                    theTask = m_currentTask; 
                } 
            }       
            if (theTask != null) 
            { 
                theTask.Start();   
                theTask.ContinueWith( (antecedent) => 
                    { 
                        Exception theEx = antecedent.Exception; 
                        if (theEx == null && antecedent is Task<Task>) 
                            theEx = (antecedent as Task<Task>)?.Result.Exception; 
 
                        if (m_onExeptionDelegate != null && theEx != null) 
                        { 
                            try { m_onExeptionDelegate(theEx); } catch(Exception) {} 
                        } 
 
                        lock(m_lock) 
                        { 
                            m_currentTask = null; 
                        } 
 
                        Task.Run( () =>  StartNextTask() ); 
                } 
            } 
        } 
    } 

我是这样使用的:

     Func<Task> action = async () => 
                { 
                   Log("Entered"); 
                   await Task.Delay(5000); 
                   Log("Exited"); 
                } 
 
m_taskSequentialQueue.EnqueueTask( action ); 
m_taskSequentialQueue.EnqueueTask( action ); 

我希望我的日志显示为:

Entered 
Exited 
Entered 
Exited 

相反,我得到:

Entered 
Entered 
Exited 
Exited 

我不确定我做错了什么。

谢谢

请您参考如下方法:

当你做 theTask.ContinueWith(在StartNextTask你继续做的事情是内在任务的开始,而不是内在任务的完成。一旦内部任务命中第一个 await theTask任务将被视为完成,因为函数已返回。

作为创可贴你可以做

if (theTask != null) 
{ 
    theTask.Start();   
    if(theTask is Task<Task>) 
    { 
       theTask = ((Task<Task>)theTask).Unwrap(); 
    } 
    theTask.ContinueWith(... 

但是我认为您使用“Cold Tasks”的整个方法是有缺陷的。你应该只使用 Queue<Func<Task>>而不是 Queue<Task>这将使您的代码更加简单。