• 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;
            }
        }
    }

     分享图片

    相关文章
    相关标签/搜索
    4887铁筼结果开奖结果小说