当前位置: 代码迷 >> C# >> 本拉灯,还是那个有关问题,快出来解惑了!
  详细解决方案

本拉灯,还是那个有关问题,快出来解惑了!

热度:23   发布时间:2016-05-05 04:16:35.0
本拉灯,还是那个问题,快出来解惑了!!!!

    public partial class ZXGC : Form
    {

        Queue<IPModel> lm = new Queue<IPModel>();
        public ZXGC()
        {
            InitializeComponent();
            //CheckForIllegalCrossThreadCalls = true;
        }

        private void ZXGC_Load(object sender, EventArgs e)
        {
            SelectIP();
            int line_count = lm.Count;
            Thread[] line_PING = new Thread[line_count];

            //MessageBox.Show(ipm.ipAd);  下面是我创建的线程
      for (int i = 0; i < line_count; i++)
            {
                foreach (IPModel ipm in lm)
                {
                    //MessageBox.Show(ipm.ipAd+"        "+ipm.id);
                    line_PING[i] = new Thread(delegate() { PingIP(ipm.ipAd, ipm.id); });
                    line_PING[i].Start();
                    Thread.Sleep(100);
                    //MessageBox.Show("线程" + i + "正在执行!" + ipm.ipAd + "      " + ipm.id);
                    break;
                }
            }
        }
            
        /// <summary>
        /// pingIP地址
        /// </summary>
        public void PingIP(string ip, int id)
        {
            while (lm.Count > 0)
            {
                lock (lm)
                {
                    IPModel m = lm.Dequeue();
                    Ping pingSender = new Ping();
                    PingReply reply = pingSender.Send(ip, 150);
                    if (reply.Status == IPStatus.Success)
                    {
                        //ping通 
                        // MessageBox.Show("OK");
                        string upIp = "UPDATE t_jl0020_pst_no set ping_ok='Y' where [email protected]_num";
                        SqlParameter[] par = new SqlParameter[] 
                        {
                         new SqlParameter("@id_num",id)
                         };
                        SQLHelper.Update(upIp, par);
                    }
                    else
                    {
                        //ping不通    
                        // MessageBox.Show("No!");
                        string upIp = "UPDATE t_jl0020_pst_no set ping_ok='N' where [email protected]_num";
                        SqlParameter[] par = new SqlParameter[] 
                        {
                         new SqlParameter("@id_num",id)
                         };
                        SQLHelper.Update(upIp, par);

                    }
                    //MessageBox.Show(woxx);
                }
            }
        }
        /// <summary>
        /// 获取车间所有测试机的IP地址
        /// </summary>
        public void SelectIP()
        {
            DataTable dt = SQLHelper.SelectTable("select id_num,com_ip from t_jl0020_pst_no");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IPModel im = new IPModel();
                im.id = Convert.ToInt32(dt.Rows[i][0].ToString());
                im.ipAd = dt.Rows[i][1].ToString();
                lm.Enqueue(im);
            }
        }

------解决思路----------------------


 Queue<IPModel> lm = new Queue<IPModel>();
        public ZXGC()
        {
            InitializeComponent();
            //CheckForIllegalCrossThreadCalls = true;
        }
 
             
        /// <summary>
        /// pingIP地址
        private void ZXGC_Load(object sender, EventArgs e)
        {
            SelectIP();
            int thread_count = Environment.ProcessorCount;
            Thread[] threads = new Thread[thread_count];

            //MessageBox.Show(ipm.ipAd);  下面是我创建的线程
            for (int i = 0; i < thread_count; i++)
            {
                threads[i] = new Thread(new ThreadStart(ProcPing));
                threads[i].IsBackground = true;
                threads[i].Start();
            }
        }

        private IPModel QueueModel()
        {
            
            lock (lm)
            {
                if (lm.Count > 0)
                {
                    IPModel m = lm.Dequeue();
                    return m;
                }
            }
            return null;
        }

        /// </summary>
        public void ProcPing()
        {
            while (lm.Count > 0)
            {
                IPModel m = QueueModel();
                if (m != null)
                {
                    Ping pingSender = new Ping();
                    PingReply reply = pingSender.Send(m.ipAd, 150);
                    if (reply.Status == IPStatus.Success)
                    {
                        //ping通 
                        // MessageBox.Show("OK");
                        string upIp = "UPDATE t_jl0020_pst_no set ping_ok='Y' where [email protected]_num";
                        SqlParameter[] par = new SqlParameter[] 
                        {
                         new SqlParameter("@id_num",m.id)
                         };
                        SQLHelper.Update(upIp, par);
                    }
                    else
                    {
                        //ping不通    
                        // MessageBox.Show("No!");
                        string upIp = "UPDATE t_jl0020_pst_no set ping_ok='N' where [email protected]_num";
                        SqlParameter[] par = new SqlParameter[] 
                        {
                         new SqlParameter("@id_num",m.id)
                         };
                        SQLHelper.Update(upIp, par);

                    }
                }
                //MessageBox.Show(woxx);
            }
        }
        /// <summary>
        /// 获取车间所有测试机的IP地址
        /// </summary>
        public void SelectIP()
        {
            DataTable dt = SQLHelper.SelectTable("select id_num,com_ip from t_jl0020_pst_no");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IPModel im = new IPModel();
                im.id = Convert.ToInt32(dt.Rows[i][0].ToString());
                im.ipAd = dt.Rows[i][1].ToString();
                lm.Enqueue(im);
            }
        }
    }




  相关解决方案