控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

利用多线程实现对网站状态的监控

作者:佚名 出处:互联网 2010年12月02日 阅读:

利用多线程实现对网站状态的监控
 
     如果有200个站点,通过Ping 测试站点是否正常,如果每次ping用时为0.5秒,那么至少要花掉100秒,这是个漫长的等待时间,所以可以考虑用多线程来实现。

    注意:使用C#的PingReply 即使返回结果不是IPStatus.Success,站点也有可能访问,因为空间商有可能关闭ICMP服务,或者有防火墙阻止了ping的包。最好是再使用WebClient做一起请求,看是否成功。

   核心代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Collections;
using Super.Ft.IDAL;
using Super.Ft.DBFactory;
using Super.Ft.Entity;
using System.Data;
using System.Threading;
using System.Net;
using System.IO;

namespace Super.Ft.BLL
{
    public class ThreadPing
    {
        public SiteConditionEntity SCEntity { get; set; }

        public SiteInfoEntity SiteEntity { get; set; }

        public ThreadPing(SiteInfoEntity s)
        {
            SiteEntity = s;
            SCEntity = new SiteConditionEntity();
        }

        public void search()
        {
            Ping p = new Ping();
            SCEntity.Siteinfoid = SiteEntity.SiteInfoID;
            SCEntity.SiteUrl = SiteEntity.SiteUrl;
            SCEntity.SiteName = SiteEntity.SiteName;
            SCEntity.MonitorDate = DateTime.Now;
            SCEntity.ADDTIME = DateTime.Now;
            try
            {
                PingReply reply = p.Send(SiteEntity.SiteUrl.Replace("http://", string.Empty).Replace(" ", ""));
                StringBuilder sbuilder = new StringBuilder();
                if (reply.Status == IPStatus.Success)
                {
                    SCEntity.Delaytime = reply.RoundtripTime;//延时
                    SCEntity.MonitorCondition = reply.RoundtripTime  "正常";
                }
                else
                {
                    SCEntity.Delaytime = 0;
                    SCEntity.MonitorCondition = "ping不通";
                    ////异常记录
                    //try
                    //{
                    //    WebClient web = new WebClient();
                    //    Stream data = web.OpenRead(SiteEntity.SiteUrl);
                    //    SCEntity.Delaytime = 0;
                    //    SCEntity.MonitorCondition = "正常";
                    //}
                    //catch (Exception)
                    //{

                    //    SCEntity.Delaytime = -1;
                    //    SCEntity.MonitorCondition = "异常";
                    //}
                
                   // byte[] buffer = web.DownloadData(SiteEntity.SiteUrl);
                  //  string aa = System.Text.Encoding.GetEncoding("gb2312").GetString(buffer);
                    //if (aa.Length > 0)
                    //{
                    //    SCEntity.Delaytime = 0;
                    //    SCEntity.MonitorCondition = "正常";
                    //}
                    //else
                    //{
                    //    SCEntity.Delaytime = -1;
                    //    SCEntity.MonitorCondition = "异常";
                    //}
                }
            }
            catch
            {
                //异常记录
                SCEntity.Delaytime = -1;
                SCEntity.MonitorCondition = "异常";
            }
        }
    }

    public class ThreadManage
    {
        Thread[] t;
        public List<SiteInfoEntity> SiteEntityList { get; set; }

        public List<SiteConditionEntity> SCEntityList { get; set; }
        public ThreadManage(List<SiteInfoEntity> list)
        {
            t = new Thread[list.Count];
            SiteEntityList = list;
            SCEntityList = new List<SiteConditionEntity>();

        }

        public void Start()
        {
            ThreadPing wp;
            for (int lIdx = 0; lIdx < SiteEntityList.Count; lIdx++)
            {
                wp = new ThreadPing(SiteEntityList[lIdx]);

                SCEntityList.Add(wp.SCEntity);
                t[lIdx] = new Thread(new ThreadStart(wp.search));
                // start the Thread object, which executes the search().
                t[lIdx].Start();
            }

            for (int lIdx = 0; lIdx < SiteEntityList.Count; lIdx++)
            {
                // waiting for all the Threads to finish.
                t[lIdx].Join();
            }
        }
    }


}

前段调用如下:

public static List<SiteConditionEntity> LiveMeshPingSiteInfo()
     {
       
         List<SiteInfoEntity> SiteEntityList = SiteInfoBLL.LoadSiteList();
         DateTime lStarted = DateTime.Now;
         ThreadManage tm = new ThreadManage(SiteEntityList);
    
         tm.Start();
     
         TimeSpan _timeSpent = DateTime.Now.Subtract(lStarted);
         return tm.SCEntityList;


      
     } 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统