• C# 子线程回调函数通知主线程完成情况

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;

    namespace ConsoleApplication5
    {
        public class ThreadCallBackTest
        {
            /// <summary>
            /// 记录正常完成的子线程数
            /// </summary>
            int count = 0;

            /// <summary>
            /// 主函数
            /// </summary>
            /// <returns></returns>
            public bool Run()
            {
                //总线程数
                int total = 10;
                MyParam m;
                List<Thread> listThread = new List<Thread>();
                for (int i = 0; i < total; i++)
                {
                    m = new MyParam();
                    m.x = i;
                    m.y = i + 1;
                    m.callBack = ThreadCallBack;
                    Thread t = new Thread(new ParameterizedThreadStart(Buss));
                    t.Start(m);
                    listThread.Add(t);
                }

                //等待所有线程完成
                foreach (var item in listThread)
                {
                    item.Join();
                }
                
                //验证子线程完成情况
                if (count == total)
                {
                    Console.WriteLine("全部完成");
                    return true;
                }
                else
                {
                    Console.WriteLine("未全部完成,count=" + count);
                    return false;
                }
            }
            
            /// <summary>
            /// 子线程函数(业务处理)
            /// </summary>
            /// <param name="param"></param>
            private void Buss(object param)
            {
                try
                {
                    MyParam myparam = (MyParam)param;
                    Thread.Sleep(2000);

                    if (myparam.x == 5)
                    {
                        throw new Exception("5555555");
                    }
                    else
                    {
                        //业务处理
                        Console.WriteLine(string.Format("第{0}个线程,{0}+{1}={2}", myparam.x, myparam.y, myparam.x + myparam.y));
                        ThreadCallBackDelegate cb = myparam.callBack as ThreadCallBackDelegate;
                        cb();
                    }
                }
                catch (Exception ex)
                {
                    //子线程异常捕获,防止子线程异常导致主程序崩溃
                    Console.WriteLine(ex.ToString());
                }
            }

            /// <summary>
            /// 回调函数,业务处理正常、完成后调用,异常不调用
            /// </summary>
            private void ThreadCallBack()
            {
                count++;
            }

            /// <summary>
            /// 定义委托
            /// </summary>
            private delegate void ThreadCallBackDelegate();

            /// <summary>
            /// 子线程参数
            /// </summary>
            class MyParam
            {
                public int x, y;
                public ThreadCallBackDelegate callBack;
            }
        }
    }

     分享图片

    相关文章
    相关标签/搜索
    天空彩天下彩正版资料 仁寿县| 班戈县| 张北县| 前郭尔| 瑞金市| 新宾| 石楼县| 古田县| 峨边| 宜宾县| 德令哈市| 河西区| 平陆县| 津市市| 南川市| 兴仁县| 汉川市| 上犹县| 峨眉山市| 仁寿县| 武强县| 区。| 白城市| 博爱县| 凤翔县| 黑河市| 霍邱县| 广平县| 枣强县| 额敏县| 威信县| 东莞市| 巴彦淖尔市| 西乌珠穆沁旗| 浑源县| 老河口市| 长子县| http://fa.hz0j0r1vo.fun http://fa.hz0j1r9vo.fun http://fa.hz0j2r0vo.fun http://fa.hz0j1r6vo.fun http://fa.hz0j2r8vo.fun