⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monitorinterface.cs

📁 监控系统
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;

using MonitorSystem.BasicClass;
using MonitorSystem.LogFileModule;

namespace MonitorSystem.MonitorInterface
{
	public class InterfaceService : System.ServiceProcess.ServiceBase
	{
		private InitLogQueue m_queInitLog;
		private RecordQueue m_queRecord;
		private UploadLogQueue m_queUploadLog;

		private DownloadRuleThread m_thrDownloadRule;
		private LogToQueueThread m_thrLogToQueue;
		private RecordToQueueThread m_thrRecordToQueue;
		private RecordParseThread []m_thrRecordParse;
		private UploadLogThread m_thrUploadLog;
		private NodeCheckThead m_thrNodeCheck; 
		private LogPrintThread logPrintThread;

		private ConfigFile m_CfgFile;
		private SystemLog m_SysLog;
		private LogFile m_LogFile;
		private int ThreadCount = int.Parse(System.Configuration.ConfigurationSettings.AppSettings["MonitorCenter.ReadLog.RecordPraseThreadNum"]);
		public static bool[] m_bNodeCheck;

		/// <summary> 
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public InterfaceService()
		{
			// 该调用是 Windows.Forms 组件设计器所必需的。
			InitializeComponent();

			this.InitMonInterface();
		}

		// 进程的主入口点
		static void Main()
		{
			System.ServiceProcess.ServiceBase[] ServicesToRun;
	
			// 同一进程中可以运行多个用户服务。若要将
			// 另一个服务添加到此进程,请更改下一行
			// 以创建另一个服务对象。例如,
			//
			//   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
			//
			ServicesToRun = new System.ServiceProcess.ServiceBase[] { new InterfaceService() };

			System.ServiceProcess.ServiceBase.Run(ServicesToRun);
		}

		/// <summary> 
		/// 设计器支持所需的方法 - 不要使用代码编辑器 
		/// 修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// InterfaceService
			// 
			this.CanPauseAndContinue = true;
			this.CanShutdown = true;
			this.ServiceName = "InterfaceService";

		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// 设置具体的操作,以便服务可以执行它的工作。
		/// </summary>
		protected override void OnStart(string[] args)
		{
			this.Start();
		}
 
		/// <summary>
		/// 停止此服务。
		/// </summary>
		protected override void OnStop()
		{
			this.Stop();
		}

		/// <summary>
		/// 暂停此服务
		/// </summary>
		protected override void OnPause()
		{
			this.Pause();
		}

		/// <summary>
		/// 继续服务
		/// </summary>
		protected override void OnContinue()
		{
			this.Resume();
		}

		protected override void OnShutdown()
		{
			this.Stop();
		}

		private void InitMonInterface()
		{
			m_SysLog = new SystemLog();

			m_CfgFile = new ConfigFile();
			while(m_CfgFile.InitSysPath()==-1)
			{
				Thread.Sleep(60000);
			}

			// 初始化对象
			this.m_LogFile = new LogFile();
			this.m_queInitLog = new InitLogQueue();
			this.m_queRecord = new RecordQueue();
			this.m_queUploadLog = new UploadLogQueue();

			// 将m_bNodeCheck的成员初始化为false,如有文件上传就将相应的m_bNodeCheck[i]置为true
			m_bNodeCheck = new bool[m_CfgFile.NodeID.Length];
			for(int i=0; i<m_bNodeCheck.Length; i++)
			{
				m_bNodeCheck[i] = false;
			}
		}


		#region 线程启动
		private void Start()
		{
			try
			{
				m_thrDownloadRule = new DownloadRuleThread();
				if(m_thrDownloadRule==null)
				{
					throw new Exception("m_thrDownloadRule failed");
				}
				m_thrDownloadRule.Init(m_CfgFile);
				m_thrDownloadRule.Startup();

				m_thrLogToQueue=new LogToQueueThread();
				if(m_thrLogToQueue==null)
				{
					throw new Exception("m_thrLogToQueue failed");
				}
				m_thrLogToQueue.Init(m_CfgFile,ref m_queInitLog);
				m_thrLogToQueue.Startup();

				m_thrRecordToQueue = new RecordToQueueThread();
				if (m_thrRecordToQueue == null)
				{
					throw new Exception("m_thrRecordToQueue failed");
				}
				m_thrRecordToQueue.Init(m_CfgFile,ref m_queInitLog,ref m_queRecord);
				m_thrRecordToQueue.Startup();


				
				m_thrRecordParse = new RecordParseThread[ThreadCount];
				for(int i=0;i<ThreadCount;i++)
				{
					m_thrRecordParse[i] = new RecordParseThread();
					if (m_thrRecordParse[i] == null)
					{
						throw new Exception("m_thrRecordParse failed");
					}
					m_thrRecordParse[i].Init(ref m_queRecord,ref m_queUploadLog,m_CfgFile,ref m_LogFile);
					m_thrRecordParse[i].Startup();
				}

				

				m_thrUploadLog = new UploadLogThread();
				if (m_thrUploadLog == null)
				{
					throw new Exception("m_thrUploadLog failed");
				}
				m_thrUploadLog.Init(ref m_queUploadLog,m_CfgFile);
				m_thrUploadLog.Startup();

				m_thrNodeCheck = new NodeCheckThead();
				if (m_thrNodeCheck == null)
				{
					throw new Exception("m_thrNodeCheck failed");
				}

				logPrintThread = new LogPrintThread();
				if (logPrintThread == null)
				{
					throw new Exception("logPrintThread failed");
				}
				logPrintThread.Startup();


				m_thrNodeCheck.Init(ref m_queRecord,m_CfgFile);
				m_thrNodeCheck.Startup();

			}
			catch(Exception ex)
			{
				this.m_SysLog.WriteToSysLog(ex.Message);
				this.Stop();
			}
		}
		#endregion

		#region 线程关闭、挂起、继续
		private void Stop()
		{
			try
			{
				if(m_thrDownloadRule != null)
					m_thrDownloadRule.SetExit();
				if(m_thrDownloadRule != null)
					m_thrDownloadRule.Resume();	
				if(m_thrDownloadRule != null)
				{
					if(m_thrDownloadRule.IsAlive())
						m_thrDownloadRule.Join();
				}

				if(m_thrLogToQueue != null)
					m_thrLogToQueue.SetExit();
				if(m_thrLogToQueue != null)
					m_thrLogToQueue.Resume();	
				if(m_thrLogToQueue != null)
				{
					if(m_thrLogToQueue.IsAlive())
						m_thrLogToQueue.Join();
				}

				if(m_thrRecordToQueue != null)
					m_thrRecordToQueue.SetExit();
				if(m_thrRecordToQueue != null)
					m_thrRecordToQueue.Resume();	
				if(m_thrRecordToQueue != null)
				{
					if(m_thrRecordToQueue.IsAlive())
						m_thrRecordToQueue.Join();
				}

				
				
				for(int i=0;i<ThreadCount;i++)
				{
					if(m_thrRecordParse[i] != null)
						m_thrRecordParse[i].SetExit();
					if(m_thrRecordParse[i] != null)
						m_thrRecordParse[i].Resume();
	
					if(m_thrRecordParse[i] != null)
					{
						if(m_thrRecordParse[i].IsAlive())
							m_thrRecordParse[i].Join();
					}
				}

				if(m_thrUploadLog != null)
					m_thrUploadLog.SetExit();
				if(m_thrUploadLog != null)
					m_thrUploadLog.Resume();	
				if(m_thrUploadLog != null)
				{
					if(m_thrUploadLog.IsAlive())
						m_thrUploadLog.Join();
				}

				if(m_thrNodeCheck != null)
					m_thrNodeCheck.SetExit();
				if(m_thrNodeCheck != null)
					m_thrNodeCheck.Resume();	
				if(m_thrNodeCheck != null)
				{
					if(m_thrNodeCheck.IsAlive())
						m_thrNodeCheck.Join();
				}

				if(logPrintThread != null)
					logPrintThread.SetExit();
				if(logPrintThread != null)
					logPrintThread.Resume();
				if(logPrintThread != null)
				{
					if(logPrintThread.IsAlive())
						logPrintThread.Join();
				}

				this.m_queInitLog.Clear();
				this.m_queRecord.Clear();
				this.m_queUploadLog.Clear();
			}
			catch(Exception ex)
			{
				m_SysLog.WriteToSysLog(2, ex.Message);
			}
		}

		private void Pause()
		{
			try
			{
				m_thrDownloadRule.Suspend();
				m_thrLogToQueue.Suspend();
				m_thrRecordToQueue.Suspend();
				for(int i=0;i<ThreadCount;i++)
				{
					m_thrRecordParse[i].Suspend();
				}
				m_thrUploadLog.Suspend();
				m_thrNodeCheck.Suspend();
				logPrintThread.Suspend();
			}
			catch(Exception ex)
			{
				m_SysLog.WriteToSysLog(2, ex.Message);
			}
		}

		private void Resume()
		{
			try
			{
				m_thrDownloadRule.Resume();
				m_thrLogToQueue.Resume();
				m_thrRecordToQueue.Resume();
				for(int i=0;i<ThreadCount;i++)
				{
					m_thrRecordParse[i].Resume();
				}
				m_thrUploadLog.Resume();
				
				m_thrNodeCheck.Resume();
				logPrintThread.Resume();
			}
			catch(Exception ex)
			{
				m_SysLog.WriteToSysLog(2, ex.Message);
			}
		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -