c# - TPL执行相关任务并在某些任务失败时停止所有任务

标签 c# .net task-parallel-library async-await

我有一个任务列表,很少有任务依赖于其他任务,我想现在运行这些任务,如果任何任务在执行过程中失败,需要停止所有正在运行的任务并关闭应用程序。

如何使用 TPL 做到这一点? 如何停止正在运行的任务? 我需要优化下面的代码。

详细要求 - 将登录屏幕作为一项任务启动。 o 仅当登录成功时才并行运行所有其他任务。 o 登录失败或取消时退出应用程序 - 如果任何任务失败,则退出应用程序

        var done = new List<TaskDetails>();
        var executing = new List<TaskDetails>();
        var unblocked = new List<TaskDetails>();
        var blocked = new List<TaskDetails>();
        foreach (var startupTest in startupTests) {
            if (startupTest.DependsOn == null) {
                unblocked.Add(startupTest);
            } else {
                blocked.Add(startupTest);
            }
        }

        IDictionary<int, TaskDetails> tasksByID = new Dictionary<int, TaskDetails>();
        var tasksTPL = new Task<object>[startupTests.Count];
        var taskCount = 0;
        var cancellationTokenSource = new CancellationTokenSource();
        var cancellationToken = cancellationTokenSource.Token;

        while (done.Count < startupTests.Count) {
            while (executing.Count < config.MaximumConcurrency && unblocked.Count > 0) {
                TaskDetails nextTask = unblocked[0];
                lock (syncLock) {
                    unblocked.Remove(nextTask);
                    executing.Add(nextTask);
                }
                // Execute
                try {
                    var method = GetMethod(
                        nextTask.AssemblyName, nextTask.ClassName, nextTask.MethodName
                    );
                    if (method == null) {
                        throw new Exception("Method" + nextTask.MethodName + " not available.");
                    }
                    tasksTPL[taskCount] = 
                        Task<object>.Factory.StartNew(() => method.Invoke(null, null), 
                        cancellationToken);
                    tasksByID.Add(tasksTPL[taskCount].Id, nextTask);
                    tasksTPL[taskCount].ContinueWith(tsk => {
                        lock (syncLock) {
                            done.Add(tasksByID[tsk.Id]);
                            executing.Remove(tasksByID[tsk.Id]);
                        }
                        if (tsk.Exception != null) {
                            TraceAlways(
                                "Caught Exception while running startuptest: " +
                                tsk.Exception
                            );
                        }
                    });
                    taskCount++;
                } catch (TargetInvocationException e) {
                    TraceAlways(
                        "Failed running " + nextTask.MethodName + " method." + e.Message);
                }
            }
            Task.WaitAny(tasksTPL.Where(task => task != null).ToArray());
            var toRemove = new List<TaskDetails>();
            lock (syncLock) {
                List<string> doneTaskName = 
                    done.Select(TaskDetails => TaskDetails.Name).ToList();
                foreach (var task in blocked) {
                    bool isBlocked = task.DependsOn.Any(dep => !doneTaskName.Contains(dep));
                    if (!isBlocked) {
                        toRemove.Add(task);
                        unblocked.Add(task);
                    }
                }
                foreach (var TaskDetails in toRemove) {
                    blocked.Remove(TaskDetails);
                }
            }
            if (executing.Count == 0 && unblocked.Count == 0 && blocked.Count > 0) {
                throw new Exception("Cyclic Dependency");
            }
        }
        taskCount = 0;
        foreach (var task in tasksTPL) {
            if (
                (task.Status != TaskStatus.Faulted) && 
                (task.Result is bool) && 
                (!(bool)task.Result)
            ) {
                TraceAlways("Startup Test" + startupTests[taskCount].MethodName + " failed.");
                if (startupTests[taskCount].ShowNotification) {
                    cancellationTokenSource.Cancel();
                    MessageBox.Show(
                        "An error has accoured. See log for more details.", "Startup Error"
                    );
                }
                Environment.Exit(0);
                break;
            }
            taskCount++;
        }

最佳答案

下面是我如何从概念上实现它(如果我正确理解了问题),尽管我并没有尝试满足您的所有详细要求。

  • 该代码未使用 ContinueWith
  • DoTaskAsync 是一个单独的独立任务。
  • DoTaskSequenceAsync 是一个任务序列,其中某些任务依赖于其他任务的结果。
  • BadTaskAsync 是抛出任务的示例,其失败应取消所有其他待处理任务。
  • WrapAsync 使用 try/catch 包装任务以捕获任务的异常并从内部引发全局取消。
  • 所有任务还支持从外部全局取消。

您可以将其作为控制台应用程序进行编译和尝试。

using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;

namespace MultipleTasks
{
    class Program
    {
        class Worker
        {
            // a single async Task
            async Task<object> DoTaskAsync(string id, CancellationToken token, int delay)
            {
                Console.WriteLine("Task: " + id);
                await Task.Delay(delay, token); // do some work
                return id;
            }

            // DoTaskSequenceAsync depends on Task1, Task2, Task3
            async Task<object> DoTaskSequenceAsync(string id, CancellationToken token)
            {
                Console.WriteLine("Task: " + id);
                await DoTaskAsync(id + "." + "Task1", token, 1000);
                await DoTaskAsync(id + "." + "Task2", token, 2000);
                await DoTaskAsync(id + "." + "Task3", token, 3000);
                // do more
                return id;
            }

            // a bad task which throws 
            async Task<object> BadTaskAsync(string id, CancellationToken token, int delay)
            {
                Console.WriteLine("Task: " + id);
                await Task.Delay(delay, token);
                throw new ApplicationException(id);
            }

            // wraps a task and requests the cancellation if the task has failed 
            async Task<T> WrapAsync<T>(CancellationTokenSource cts,
                Func<CancellationToken, Task<T>> taskFactory)
            {
                try
                {
                    return await taskFactory(cts.Token);
                }
                catch
                {
                    if (!cts.IsCancellationRequested)
                    {
                        cts.Cancel(); // cancel the others
                    }
                    throw; // rethrow
                }
            }

            // run all tasks
            public async Task DoWorkAsync(CancellationToken outsideCt)
            {
                var tasks = new List<Task<object>>();

                var cts = new CancellationTokenSource();

                ExceptionDispatchInfo capturedException = null;

                try
                {
                    using (outsideCt.Register(() => cts.Cancel()))
                    {
                        // these tasks run in parallel
                        tasks.Add(WrapAsync(cts, (token) => DoTaskAsync("Task1", token, 500)));
                        tasks.Add(WrapAsync(cts, (token) => DoTaskSequenceAsync("Sequence1", token)));
                        tasks.Add(WrapAsync(cts, (token) => DoTaskAsync("Task2", token, 1000)));
                        tasks.Add(WrapAsync(cts, (token) => BadTaskAsync("BadTask", token, 1200)));
                        tasks.Add(WrapAsync(cts, (token) => DoTaskSequenceAsync("Sequence2", token)));
                        tasks.Add(WrapAsync(cts, (token) => DoTaskAsync("Task3", token, 1500)));

                        await Task.WhenAll(tasks.ToArray());
                    }
                }
                catch (Exception e)
                {
                    capturedException = ExceptionDispatchInfo.Capture(e);
                }

                if (outsideCt.IsCancellationRequested)
                {
                    Console.WriteLine("Cancelled from outside.");
                    return;
                }

                if (cts.IsCancellationRequested || capturedException != null)
                {
                    if (cts.IsCancellationRequested)
                    {
                        Console.WriteLine("Cancelled by a failed task.");
                        // find the failed task in tasks or via capturedException
                    }
                    if (capturedException != null && capturedException.SourceException != null)
                    {
                        Console.WriteLine("Source exception: " + capturedException.SourceException.ToString());
                        // could rethrow the original exception:
                        // capturedException.Throw();                   
                    }   
                }

                Console.WriteLine("Results:");
                tasks.ForEach((task) =>
                        Console.WriteLine(String.Format("Status: {0}, result: {1}",
                            task.Status.ToString(), 
                            task.Status == TaskStatus.RanToCompletion? task.Result.ToString(): String.Empty)));
            }
        }

        static void Main(string[] args)
        {
            var cts = new CancellationTokenSource(10000);
            new Worker().DoWorkAsync(cts.Token).Wait();
            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
}

关于c# - TPL执行相关任务并在某些任务失败时停止所有任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19394150/

相关文章:

c# - 将 LINQ 转换为 XML

c# - 在 C# 中从 FTP 服务器下载名称包含特殊字符的文件

c# - 如何正确关闭持久的 System.Net.WebSockets.ClientWebSocket 连接?

c# - 在 WCF 或 WebAPI 方法 (IIS) 中返回 "Task<int>"而不是 "int"的好处

javascript - 突出显示选定的表格单元格 C# 菜单

c# - MailItem SaveAs() 方法文件类型

c# - AutoFixture:配置开放式泛型样本生成器

c# - 创建 protected 链接

c# - LINQ to XML 无法获取元素

c# - 在 Windows 窗体中运行长任务时保持 UI 线程响应