zoukankan      html  css  js  c++  java
  • 服务程序监控配置是否更改

    服务程序(Service1.cs)
            protected override void OnStart(string[] args)
            {
                //调试用
                //System.Threading.Thread.Sleep(15000);
                log4net.Config.XmlConfigurator.ConfigureAndWatch( new System.IO.FileInfo(Sxmobi.FileHelper.GetMapPath("")+ "log4net.config"));
                _timer = new System.Timers.Timer(_interval);
                _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
                _timer.Start();


            }

           void _timer_Elapsed(object sender, ElapsedEventArgs e)
            {
                //要做的事情
                this._timer.Enabled = false;

                try
                {
                    while (true)
                    {

                        Sxmobi.LogHelper.Info(this.GetType().ToString(), "VP2抓取程序开始", null);
                        TaskProc tp = new TaskProc();
                        tp.LoadTask();
                        WaitHandle.WaitAll(tp.waitHandles);//配置文件更改后此语句会执行

                    }
                }
                catch (Exception ex)
                {
                    Sxmobi.LogHelper.Error(this.GetType().ToString() ,ex.Message,ex);
                    Sxmobi.LogHelper.Info(this.GetType().ToString(), "VP2抓取程序已停止", null);
                }

            }

    监控程序(TaskProc.cs)

            public WaitHandle[] waitHandles;
            public TaskProc()
            {
            }

            public void LoadTask()
            {
                ListParser lp = new ListParser();
                ListParser.iCurrent++;//结束上一个线程
                new Thread(new ThreadStart(lp.RunBat)).Start();
                JianKong();
            }


            void JianKong()
            {
                this.waitHandles = new WaitHandle[] { new AutoResetEvent(false) };
                AutoResetEvent are = (AutoResetEvent)this.waitHandles[0];
                do
                {
                    Thread.Sleep(10000);
                }
                while (!SiteConfig.ConfigIsUpdate);//配置文件更改后执行下面语句

                are.Set();
            }


     

     主程序(ListParser.cs)

    class ListParser
        {
            public static int iCurrent = 0;
            public ListParser() { }

            public void RunBat()
            {
                int ic = iCurrent;
                while (ic==iCurrent)//是否是同一线程
                {
                    RutAllFiles(SiteConfig.BatFileRoot);
                    Thread.Sleep(1000);
                }

            }

            private void RutAllFiles(string fileDirectory)
            {
                DirectoryInfo diSource = new DirectoryInfo(fileDirectory);
                FileSystemInfo[] fsi = diSource.GetFileSystemInfos();
                FileInfo fi;

                try
                {
                    for (int i = 0; i < fsi.Length; i++)
                    {
                        //不是目录,查看文件属性;是目录,继续遍历。
                        if (Directory.Exists(fsi[i].FullName) == false)
                        {
                            fi = new FileInfo(fsi[i].FullName);
                            //判断文件类型,进行相应的后继操作。
                            if (fi.Extension == ".bat")
                            {
                                try
                                {
                                    Process p = new Process();
                                    ProcessStartInfo psi = new ProcessStartInfo(fi.FullName);
                                    p.StartInfo = psi;
                                    p.Start();
                                    p.WaitForExit(30000);
                                    File.Delete(fi.FullName);
                                    Thread.Sleep(500);
                                }
                                catch (Exception ex)
                                {
                                    Sxmobi.LogHelper.Error(this.GetType().ToString(), ex.Message, ex);
                                }

                            }
                        }
                        else
                        {
                            RutAllFiles(fsi[i].FullName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Sxmobi.LogHelper.Error(this.GetType().ToString(), ex.Message, ex);

                }

            }

    读取配置程序(SiteConfig.cs)

            private static string xmlPath = Sxmobi.FileHelper.GetMapPath("~") + "\\Config\\Site.config";
            static XmlDocument xmlDoc = Sxmobi.XmlHelper.GetXmlDoc(xmlPath);

            private static DateTime LastestXmlModefyTime = Utility.getFileModifyTime(xmlPath);

            public static bool ConfigIsUpdate
            {
                get
                {
                    try
                    {
                        if ((Utility.getFileModifyTime(xmlPath) != DateTime.MinValue) && (LastestXmlModefyTime != Utility.getFileModifyTime(xmlPath)))
                        {
                            LastestXmlModefyTime = Utility.getFileModifyTime(xmlPath);
                            xmlDoc = Sxmobi.XmlHelper.GetXmlDoc(xmlPath);//重新读取config
                            return true;
                        }
                        return false;
                    }
                    catch
                    {
                        return false;
                    }
                }

            }

            public static string BatFileRoot
            {
                get
                {
                    return Sxmobi.XmlHelper.GetNodeValue(xmlDoc, "/Site/BatFileRoot");
                }
            }

  • 相关阅读:
    wpf arcgis engine 当前没有或未启用Spatial Analyst许可解决办法
    arcglobe 图层三大类说明
    sql自带函数语句
    wpf 前台获取资源文件路径问题
    Microsoft.Office.Interop.Excel的用法
    WPF:父窗口与子窗口的层次关系
    wpf 拖图片到窗体
    wpf comboBox取值问题
    wpf 窗体内容旋转效果 网摘
    js拖动滑块
  • 原文地址:https://www.cnblogs.com/dashi/p/4034753.html
Copyright © 2011-2022 走看看