zoukankan      html  css  js  c++  java
  • 《C#并行编程高级教程》第3章 命令式任务并行 笔记

    Task的使用

    var t1 = new Task(() => GenerateAESKeys());
    var t2 = new Task(() => GenerateMD5Hashes());
    // Start the tasks
    t1.Start();
    t2.Start();
    // Wait for all the tasks to finish
    Task.WaitAll(t1, t2);

    等待超时

    // Wait for all the tasks to finish with a 1 second timeout
    if (!Task.WaitAll(new Task[] { t1, t2 }, 1000))
    {
        //...
    }

    取消任务

    使用CancellationToken,在取消时会抛出OperationCanceledException异常
    private static void GenerateAESKeysCancel(System.Threading.CancellationToken ct)
    {
        ct.ThrowIfCancellationRequested();
        var aesM = new AesManaged();
        for (int i = 1; i <= NUM_AES_KEYS; i++)
        {
            aesM.GenerateKey();
            byte[] result = aesM.Key;
            string hexString = ConvertToHexString(result);
            // Console.WriteLine("AES KEY: {0} ", hexString);
            ct.ThrowIfCancellationRequested();
        }
    }

    异常处理

    try
    {
        //...
    }
    catch (AggregateException ex)
    {
        foreach (Exception innerEx in ex.InnerExceptions)
        {
            Debug.WriteLine(innerEx.ToString());
            // Do something considering the innerEx Exception
        }
    }

    任务返回值

    使用Task<TResult>创建的任务在Reault属性中可以保存返回值
    var cts = new System.Threading.CancellationTokenSource();
    var ct = cts.Token;
    var t1 = Task.Factory.StartNew(
        () => GenerateAESKeysWithCharPrefix(ct, 'A'), ct);
    t1.Wait();
    for (int i = 0; i < t1.Result.Count; i++)
    {
        Console.WriteLine(t1.Result[i]);
    }
    private static List<String> GenerateAESKeysWithCharPrefix(
        System.Threading.CancellationToken ct, char prefix)
    {
        var aesM = new AesManaged();
        var keysList = new List<String>();
        for (int i = 1; i <= NUM_AES_KEYS; i++)
        {
            aesM.GenerateKey();
            byte[] result = aesM.Key;
            string hexString = ConvertToHexString(result);
            if (hexString[0] == prefix)
            {
                keysList.Add(hexString);
            }
            if (ct.IsCancellationRequested)
            {
                ct.ThrowIfCancellationRequested();
            }
        }
        return keysList;
    }

    串联任务

    var cts = new System.Threading.CancellationTokenSource();
    var ct = cts.Token;
    var t1 = Task.Factory.StartNew(
        () => GenerateAESKeysWithCharPrefix(ct, 'A'), ct);
    var t2 = t1.ContinueWith((t) =>
    {
        for (int i = 0; i < t.Result.Count; i++)
        {
            Console.WriteLine(t.Result[i]);
        }
    });
     





  • 相关阅读:
    Python标准库 -- UUID模块(生成唯一标识)
    Python全局解释器锁 -- GIL
    Python Web Server Gateway Interface -- WSGI
    Mysql 和 Postgresql 抛开性能的对比
    一篇文章掌握RequireJS常用知识
    彻底理解js中的闭包
    全面理解Javascript闭包和闭包的几种写法及用途【转】
    JS 日期转换,格式化等常用的函数定义
    把上传过来的多张图片拼接转为PDF的实现代码
    C# Stream 和 byte[] 之间的转换(文件流的应用)
  • 原文地址:https://www.cnblogs.com/atskyline/p/3234527.html
Copyright © 2011-2022 走看看