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

📄 uploadlogthread.cs

📁 监控系统
💻 CS
字号:
using System;
using System.IO;
using System.Threading;
using MonitorSystem.BasicClass;
using MonitorSystem.FtpClient;

namespace MonitorSystem.MonitorInterface
{
	/// <summary>
	/// 定时从UploadLogQueue中取Log文件,上传至监控中心指定的目录下
	/// </summary>
	public class UploadLogThread
	{
		private Thread m_Thread;
		private bool m_Pause;
		private bool m_Exit;

		private UploadLogQueue m_UploadLogQueue;
		private ConfigFile m_CfgFile;
		private SystemLog m_SysLog;
		private FTPClass m_ftpClient;

		private string m_strUploadFile;

		public UploadLogThread()
		{
			m_SysLog = new SystemLog();
			m_Pause = false;
			m_Exit = false;
		}

		public void Init(ref UploadLogQueue iUploadLogQueue,ConfigFile iCfgFile)
		{
			this.m_UploadLogQueue = iUploadLogQueue;
			this.m_CfgFile = iCfgFile;
		}

		public void Run()
		{
			while(true)
			{
				while(m_Pause)
				{
					try
					{
						Thread.Sleep(1000);
					}
					catch(Exception)
					{						
					}
					continue;
				}
				if(m_Exit)
				{
					break;
				}

				if (m_UploadLogQueue.Count<=0)
				{
					try
					{
						Thread.Sleep(1000);
						continue;
					}
					catch(Exception ex)
					{
						string msg = ex.Message;
						m_SysLog.WriteToSysLog(1,msg);
					}
				}
				else
				{
					//取一个待上传文件
					try
					{
						this.m_strUploadFile = (string)m_UploadLogQueue.Dequeue();
					}
					catch
					{
						continue;
					}

					//上传MONITOR_LOG文件
					try
					{
						m_ftpClient = new MonitorSystem.FtpClient.FTPClass();
						m_ftpClient.setRemoteHost(m_CfgFile.CenterIP);
						m_ftpClient.setRemotePort(m_CfgFile.CenterPort);
						m_ftpClient.setRemoteUser(m_CfgFile.CenterFtpUserName);
						m_ftpClient.setRemotePass(m_CfgFile.CenterFtpPsw);
						m_ftpClient.setRemotePath(m_CfgFile.CenterLog_Path);

						m_ftpClient.login();
						m_ftpClient.upload(m_strUploadFile);
						File.Delete(m_strUploadFile);
						m_ftpClient.close();
					}
					catch(Exception ex)
					{
						string msg = String.Format("上传Monitor_Log文件失败:{0}",ex.Message);
						m_SysLog.WriteToSysLog(0, msg);
					}
				}

				if(m_Exit)
				{
					break;
				}
				try
				{
					//规则链表遍历完毕,线程休眠一分钟
					Thread.Sleep(100);
				}
				catch(Exception)
				{						
				}
								
			}			
		}

		public void Startup()
		{ 				
			m_Thread = new Thread(new ThreadStart(this.Run));
			// Start the thread
			m_Pause = false;
			m_Exit = false;		
			m_Thread.Start();			
		}
		public void Join()
		{
			if(m_Thread != null)
			{
				m_Thread.Interrupt();
				m_Thread.Join();
			}
			else
			{
				return;
			}
		}
		public bool IsAlive()
		{
			if(m_Thread != null)
			{
				return m_Thread.IsAlive;
			}
			else
			{
				return false;
			}
		}
		public void Suspend()
		{
			if(m_Pause == false)
				m_Pause = true;
		}
		public void Resume()
		{
			if(m_Pause == true)
				m_Pause = false;		
		}
		public void SetExit()
		{
			if(m_Exit == false)
				m_Exit = true;		
		}
	}
}

⌨️ 快捷键说明

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