<address id="ll1nf"><listing id="ll1nf"><mark id="ll1nf"></mark></listing></address>

    <address id="ll1nf"></address>

        <sub id="ll1nf"></sub>

        <thead id="ll1nf"><var id="ll1nf"><output id="ll1nf"></output></var></thead>

        <address id="ll1nf"><dfn id="ll1nf"></dfn></address><form id="ll1nf"><listing id="ll1nf"><mark id="ll1nf"></mark></listing></form>

          <address id="ll1nf"><dfn id="ll1nf"></dfn></address>

          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.hz0j1r6vo.fun http://fa.hz0j0r7vo.fun http://fa.hz0j1r8vo.fun http://fa.hz0j1r7vo.fun http://fa.hz0j2r5vo.fun