我的实验作业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; }


        }


    }
}

 

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