我的實驗作業20191211:多線程全部結束後執行後續操作(利用回調函數思路)

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

namespace cslTest
{
    public class T3_MyCallBack
    {

        public class myMulti 
        {
            public delegate void callBackHandle();
            List<threadInfos> tdlist = new List<threadInfos>();
            List<Thread> thdlist = new List<Thread>();
            int iCnt = 0;
            public void batchInsert()
            {
                int i = 0;
                tdlist.Clear();
                while (i<10)
                {
                    Thread td = new Thread(new ThreadStart(print));
                    td.Name = "td"+i.ToString();
                    Interlocked.Increment(ref iCnt);//執行之前,增加一個計數器
                    td.Start();
                    Console.WriteLine("啟動線程:" + i);
                    i++;
                }
            }
            public void print()//test此處用來執行資料抓取,多線程啟動
            {
                Console.WriteLine("print");
                Interlocked.Decrement(ref iCnt);//執行完一個線程,釋放一個計數器
                //完成執行更新hashtable中的記錄
                noteMyinfo();//每一個線程執行結束後去判斷是否執行下一步
            }

            public void noteMyinfo()
            {
                if (iCnt>0)
                {
                    Console.WriteLine("回調時存在未執行完的進程,不執行後續操作");
                }
                else
                {
                    Console.WriteLine("開始執行後續操作");
                }

            }
        }

        public class threadInfos
        {
            public Guid pkguid { set; get; }
            public string strTdName { set; get; }
            public bool bTdIsAlive { set; get; }
            public int iTdManagedThreadId { set; get; }

            public int iFinish { set; get; }


        }


    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章