📄 scanftpthread.cs
字号:
using System;
using System.Threading;
using System.IO;
using MonitorSystem.FtpClient;
using MonitorSystem.BasicClass;
namespace MonitorSystem.MonitorCenter
{
/// <summary>
/// ScanFTPThread 的摘要说明。
/// </summary>
public class ScanFTPThread
{
private Thread m_Thread;
private bool m_Pause;
private bool m_Exit;
private FileSystemWatcher FileWatcher;
private static LogFileQueue m_InitLogQueue;
private static ConfigFile m_ConfigFile;
private SystemLog m_SysLog;
private FTPClass ftpClient;
private FTPSite InterftpSite;
private string WatchDir; //设定监控产生LOG文件的目录
private string WatchFilter; //监控目录文件过滤器
public void Init(ref LogFileQueue iLogFileQueue, ref ConfigFile iConfigFile)
{
m_InitLogQueue = iLogFileQueue;
m_ConfigFile = iConfigFile;
m_SysLog = new SystemLog();
}
public void Run()
{
try
{
WatchDir = m_ConfigFile.MonitorLogPath ;
if(WatchDir[WatchDir.Length-1]!='\\')
WatchDir+="\\";
WatchFilter = m_ConfigFile.MonitorLogFilter;
FileWatcher = new FileSystemWatcher();
FileWatcher.Path = WatchDir; // 通过配置文件来进行指定
FileWatcher.Filter = WatchFilter;
FileWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// 添加事件响应,监控到LOG目录有新文件产生时,将文件名加入待处理队列
FileWatcher.Created += new FileSystemEventHandler(OnChanged);
FileWatcher.EnableRaisingEvents = true;
while(true)
{
#region 控制标志
while(m_Pause)
{
try
{
Thread.Sleep(1000);
}
catch(Exception)
{
}
continue;
}
if(m_Exit)
{
break;
}
#endregion
#region 下载SP流量文件
try
{
InterftpSite = new FTPSite();
ftpClient = new FTPClass();
ftpClient.setRemoteHost(m_ConfigFile.InterfaceIP);
ftpClient.setRemotePort(m_ConfigFile.InterfacePort);
ftpClient.setRemoteUser(m_ConfigFile.FtpUser);
ftpClient.setRemotePass(m_ConfigFile.FtpPsw);
ftpClient.setRemotePath(m_ConfigFile.Download_Path);
InterftpSite.FTPControl=ftpClient;
string[] fileList = InterftpSite.GetFileListFromRemote("CNT????????????.txt",m_ConfigFile.Download_Path,0);
foreach(string str in fileList)
{
InterftpSite.DealDownLoadORUpLoadFile(str,m_ConfigFile.MonitorLogPath + "\\bak\\",0,0);
File.Move(m_ConfigFile.MonitorLogPath + "\\bak\\" + str,m_ConfigFile.MonitorLogPath + "\\" + str);
File.Delete(m_ConfigFile.MonitorLogPath + "\\bak\\" + str);
}
}
catch(Exception ex)
{
m_SysLog.WriteToSysLog(ex.Message + ex.StackTrace);
}
finally
{
if(InterftpSite!=null)
{
InterftpSite=null;
}
if(ftpClient!=null)
{
ftpClient.close();
ftpClient=null;
}
}
#endregion
#region 睡眠
try
{
Thread.Sleep(60 * 1000);
}
catch(Exception)
{
}
#endregion
}
}
catch(Exception ex)
{
m_SysLog.WriteToSysLog(ex.Message);
}
}
public static void OnChanged(object source, FileSystemEventArgs e)
{
if(e.ChangeType == WatcherChangeTypes.Created )
{
if(e.Name.ToUpper().StartsWith("MONITOR"))
{
string PlatformID = e.Name.Substring(8,4);
for(int i=0; i<m_ConfigFile.PlatformID.Length; i++)
{
if(m_ConfigFile.PlatformID[i]==Convert.ToInt32(PlatformID))
{
CenterService.m_bPlatformCheck[i] = true;
m_InitLogQueue.Enqueue(e.FullPath);
}
}
}
else
{
m_InitLogQueue.Enqueue(e.FullPath);
}
}
}
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 + -