多线程

示例是一个使用c#编写的 多线程ping IP列表的小程序。
主要功能:
1.通过EXCEL导入主机列表数据;
2.通过配置文件控制程序的一些条件参数等;
3.启动多线程轮循或单次对EXCEL 导入的数据进行 ping请求,并显示状态

代码结构及类文件描述:
ThreadPool 类 线程控制的主类,线程相关控制 启、停、执行的方法等都集中的这个类中.
ExcelHelper 类 EXCEL 帮助类,实现对excel 导入、导出功能.
Log 类 日志操作类.
MessageOperation 类 消息提示类 .
ConnectionManager 类 ping 主机的具体实现类.
CommandHelper 类 c# 进行命令操作的帮助类,可执行DOS命令,(未使用).

程序附件地址:https://share.weiyun.com/6be03d694d8c046532422c4ad346add3

ThreadPool文件:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Data;
using Common;

namespace MonitorWindows
{
    public delegate void RefreshVieweState();
    public delegate void RefreshVieweDelegate(MonitorModel model,int index,int total); 

    public class CThreadPool
    {

        //存储加载的IP数据
        public static List<MonitorModel> IPList = new List<MonitorModel>();

        public CThreadPool(RefreshVieweDelegate refreshViewDelegate,
            RefreshVieweState start, RefreshVieweState end 
            ,RefreshVieweState allColse) {
            this.refreshView = refreshViewDelegate;
            this.refreshViewStart = start;
            this.refreshViewEnd = end;
            this.refreshVieAllColse = allColse;
        }

        //更新界面方法
        public RefreshVieweDelegate refreshView;   
        public RefreshVieweState refreshViewStart;
        public RefreshVieweState refreshViewEnd;
        public RefreshVieweState refreshVieAllColse;

        //当前轮询开始时间
        public DateTime PollingStartTime;
        //上次完成时间
        public DateTime PollingDoneTime;
        //轮询状态
        public PollingState pollingState = PollingState.Done;
        //启动线程,计算时间定时开启轮询
        Thread mainThread;
        bool isRuning = true; //运行标识

        bool isLoop = true;//持续轮循还是单次
        int times = 0; //已轮循次数

        //开始线程
        public void DoPolling(bool isloop) {
            if (mainThread == null || mainThread.ThreadState != ThreadState.Running)
            {
                PollingDoneTime = DateTime.MinValue;
                times = 0;
                this.isLoop = isloop;
                isRuning = true;
                mainThread = new Thread(new ThreadStart(Loop));
                mainThread.Start();
            }
        }

        private void Loop() {
            while (isRuning) {
                
                //开始
                TimeSpan ts = DateTime.Now - PollingDoneTime;
                if (ts.TotalSeconds >= ConfigHelper.FollingSpan && pollingState == PollingState.Done)
                {
                    if (StartThread()) {
                        refreshViewStart();
                    }
                   
                }
                


                //结束
                if (indexer == 0 && pollingState == PollingState.Polling) {
                    bool alldone = true;
                    foreach (Thread item in threads) {
                        if (item.ThreadState == ThreadState.Running)
                            alldone = false;
                    }
                    if (alldone)
                    {
                        PollingDoneTime = DateTime.Now;
                        pollingState = PollingState.Done;
                        refreshViewEnd();
                        if (isLoop == false && times > 0)
                        {
                            refreshVieAllColse();
                        }
                    }
                }
               

                Thread.Sleep(2000);
            }
        }
 

        //停止所有线程
        public void Close()
        {
            
            for (int i = 0; i < threads.Length; i++)
            {
                Thread thread = threads[i];
                if (thread != null && thread.ThreadState == ThreadState.Running)
                {

                    thread.Abort();
                }
            }
            

        }
        //判断是否停止完成
        public bool CheckAllClose()
        {
            bool allstop = false;
            while (allstop == false) {
                allstop = true;
                for (int i = 0; i < threads.Length; i++)
                {
                    Thread thread = threads[i];
                    if (thread != null && thread.ThreadState != ThreadState.Aborted 
                        && thread.ThreadState != ThreadState.Stopped)
                    {
                        allstop = false;
                    }
                }
                Thread.Sleep(100);
            }

            while (pollingState != PollingState.Done) { Thread.Sleep(100); }

            mainThread.Abort();

            while (mainThread != null && mainThread.ThreadState != ThreadState.Aborted
                   && mainThread.ThreadState != ThreadState.Stopped)
            {
                Thread.Sleep(100);
            }
            isRuning = false;
            mainThread = null;

            refreshVieAllColse();

            return allstop;
        }

        #region  轮询线程

        private static object lockObj = new object();
        Thread[] threads = new Thread[ConfigHelper.ThreadNum];
        private int indexer = 0;
        private int total = 0;
        PollingObject[] pollingList = null;

        public PollingObject[] PollingList
        {
            get
            {
                return pollingList;
            }
        }

        //Dictionary<string, PollingObject> dopings = new Dictionary<string, PollingObject>();  

        //填充需要轮询对象集合
        private void FillPolling() {
            lock (lockObj)
            {  
                //查询数据集获取count并加入数组
                pollingList = PollingObject.QueryFromDataBase();
                
                total = PollingList.Length;
                indexer = total; // 从后往前轮询

            }
        }
        //初始化线程
        private void InitThread()
        {

            for (int i = 0; i < threads.Length; i++) { Thread thread = threads[i]; thread = new Thread(new ThreadStart(DoMethod)); threads[i] = thread; } } //开始线程进行轮询 private bool StartThread() { if (isLoop == false && times > 0) { 
                return false;
            }

            InitThread();
            if (pollingState == PollingState.Polling)
            {
                return false;
            }
            else {
                //开始一次新轮询

                FillPolling();
                pollingState = PollingState.Polling;
                PollingStartTime = DateTime.Now;
                
            }

            foreach (Thread thread in threads)
            {
                if (thread.ThreadState == ThreadState.Unstarted
                    || thread.ThreadState == ThreadState.Stopped)
                {
                    thread.Start();
                }

            }

            times++;

            return true;
            
        }

        //线程执行的方法
        private void DoMethod() {
            PollingObject pobj = null;
            do
            {

                pobj = GetNextPolling();


                if (pobj != null)
                {
                    //dopings.Add(pobj.monitorModel.Id.ToString(), pobj);

                    //ping 对象IP
                    pobj.PingService(PollingStartTime);

                    //刷新界面
                    refreshView(pobj.monitorModel,pobj.index,total);

                    //dopings.Remove(pobj.monitorModel.Id.ToString());
                }
            } while (pobj != null && isRuning);
        }

        //获取下一个轮询对象
        private PollingObject GetNextPolling()
        {
 
            PollingObject pollingObject = null;
            lock (lockObj)
            {
                if (indexer != 0)
                {
                    indexer--;
                    pollingObject = pollingList[indexer];
                }
            }
            return pollingObject;
        }
        
        #endregion
    }

    public class PollingObject{

        
        public MonitorModel monitorModel;
        public int index;

        public PollingObject(MonitorModel model) {
            this.monitorModel = model;
        }

        public void PingService(DateTime startTime) {
            if (monitorModel == null)
                return;
 
            ConnectionManager connection = new ConnectionManager(monitorModel.Ip);
            connection.PingTimes();

            bool isonline = connection.IsOnline;

            if (isonline)
            {
                bool isdelay = connection.IsDelay;
                if (isdelay)
                {
                    monitorModel.Status = MonitorState.Delay;

                }
                else
                {
                    monitorModel.Status = MonitorState.Normal;
                }
            }
            else
            {
                monitorModel.Status = MonitorState.Lost;
            }

            

        }

        //查询出所有需要摄像头
        public static PollingObject[] QueryFromDataBase() {
            PollingObject[] objects = new PollingObject[0];
            
            if (CThreadPool.IPList  != null && CThreadPool.IPList.Count > 0) {
                objects = new PollingObject[CThreadPool.IPList.Count];
            }
            int temp = 0;
            foreach (MonitorModel model in CThreadPool.IPList) {
 
                PollingObject pollingObject = new PollingObject(model);
                pollingObject.index = temp;
                objects[temp] = pollingObject;
                temp++;
            }

            return objects;
        }

    }






}