• task code


    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;
    
    public class Example
    {
        public static void Main()
        {
            var qTasks = new List<Task<string>>();
            var paralist = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            foreach (var para in paralist)
            {
                //StartNew 只接受输入参数是object类型的Func 
                var fun = new Func<object, string>(
                    (pa) =>
                    {
                        //pa 是Func的定义参数
                        Console.WriteLine("para is {0}", pa);
                        //Thread.Sleep(new Random().Next(500, 3000));
                        int ipa = Convert.ToInt32(pa);
                        int result = ipa * 5;
                        return result.ToString();
                    }
                    );
    
                var task = Task.Factory.StartNew(fun, para); //para是传入参数
                qTasks.Add(task);
                Console.WriteLine(" task id is {0}", task.Id);
            }
    
            Task.WaitAll(qTasks.ToArray());  //等待所有线程执行完毕
    
            //收集所有task返回的数据
            foreach (var task in qTasks)
            {
                if (task.Result != null)
                {
                    Console.WriteLine("task id : {0} , result : {1} ", task.Id, task.Result);
                }
            }
    
            Console.ReadKey();
        }
    }
    

      

    有时候会需要用到ManualResetEvent来等待其他线程是否执行完毕,用法如下:

    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;
    
    public class Example
    {
        static ManualResetEvent manualEvent = new ManualResetEvent(false);
    
        public static void Main()
        {
            manualEvent.Reset(); //等同于将initialState设置为false
    
            Console.WriteLine("In main ..");
            LongTimeFunc();
    
            manualEvent.WaitOne(10000);
    
            Console.WriteLine("wait thread finish...");
    
            Console.ReadKey();
        }
    
    
        private static void LongTimeFunc()
        {
            //Thread 多数时候可以使用Task代替,此刻thread设置为STA,所以这么用
            Thread thread = new Thread(new ParameterizedThreadStart(ExecuteFunc));
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start("real parameters");    //ExecuteFunc 函数从此处传入参数
        }
    
        private static void ExecuteFunc(object obj)
        {
            //long time operation
            Thread.Sleep(2000);
            Console.WriteLine(obj.ToString());
            manualEvent.Set();
        }
    }
    

      

  • 相关阅读:
    String substring(int start,int end)截取当前字符串中指定范围内的字符串
    String字符串是不变对象,内容一旦创建不可改变,若改变一定会创建新对象
    StringBuilder修改字符串内容,增,删,改,插
    startsWith(),endsWith()判断当前字符串是否是以给定字符串开始或结尾的
    length()返回当前字符串的字符个数
    indexOf()字符位置
    charAt()检测回文
    手把手教你猜字小游戏
    release 和 drain
    c专家编程 ,c语言运行时环境
  • 原文地址:https://www.cnblogs.com/luohengstudy/p/6182914.html
Copyright © 2020-2023  润新知