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

    执行结果:

  • 相关阅读:
    Don't Dismiss Georgia Tech's $6,600 Online Master's Degree
    Java SPI机制学习笔记
    Java SPI机制简介
    Java SPI机制
    Java SPI机制原理和使用场景
    中文代码之Spring Boot集成H2内存数据库
    H2数据库攻略
    JAVA 项目中使用 H2 数据库
    Java内存数据库-H2介绍及实例(SpringBoot)
    内存数据库-H2简介与实践
  • 原文地址:https://www.cnblogs.com/wsgm/p/11724662.html
Copyright © 2020-2023  润新知