📄 recordtoqueuethread.cs
字号:
using System;
using System.IO;
using System.Threading;
using MonitorSystem.BasicClass;
using MonitorSystem.LogFileModule;
namespace MonitorSystem.MonitorInterface
{
/// <summary>
/// 从InitLogQueue中取Log文件,并逐行读出LogRecord,写入RecordQueue队列中.
/// </summary>
public class RecordToQueueThread
{
private Thread m_Thread;
private bool m_Pause;
private bool m_Exit;
private ConfigFile m_ConfigFile;
private InitLogQueue m_InitLogQueue;
private RecordQueue m_RecordQueue;
private string m_strFileName;
private string m_strLineData;
private FileStream m_fstream=null;
private StreamReader m_logReader=null;
private SystemLog m_SysLog;
public RecordToQueueThread()
{
m_SysLog = new SystemLog();
m_Pause = false;
m_Exit = false;
}
public void Init(ConfigFile iCfgFile,ref InitLogQueue queInitLog,ref RecordQueue queRecord)
{
m_ConfigFile = iCfgFile;
m_InitLogQueue = queInitLog;
m_RecordQueue = queRecord;
}
public void Run()
{
while(true)
{
while(m_Pause)
{
try
{
Thread.Sleep(1000);
}
catch(Exception)
{
}
continue;
}
if(m_Exit)
{
break;
}
if (m_InitLogQueue.Count<=0)
{
try
{
Thread.Sleep(1000);
continue;
}
catch(Exception)
{
continue;
}
}
try
{
m_strFileName = (string)m_InitLogQueue.Dequeue();
bool ReadOk=false;
int tryCount=1;
while(!ReadOk&&tryCount<=m_ConfigFile.TryCount)
{
try
{
m_fstream = new FileStream(m_strFileName,FileMode.Open);
ReadOk = true;
}
catch(Exception e)
{
Thread.Sleep(m_ConfigFile.TryReadTimeSpan);
m_SysLog.WriteToSysLog(1,"Interface:监控日志文件读取错误,重试次数:{1}\r\n原因:{0}", e.Message,tryCount);
tryCount++;
}
}
//m_fstream = new FileStream(m_strFileName,FileMode.Open);
m_logReader = new StreamReader (m_fstream,System.Text.Encoding.GetEncoding(936));
while((m_strLineData = m_logReader.ReadLine() )!= null)
{
string[] DataArry = m_strLineData.Split('\t');
LogFile logrecord = new LogFile();
logrecord.strDateTime = DataArry[0].ToString();
logrecord.iPlatformID = Convert.ToInt32(DataArry[1]);
logrecord.iNodeID = Convert.ToInt32(DataArry[2]);
logrecord.iTypeID = Convert.ToInt32(DataArry[3]);
logrecord.iRuleID = Convert.ToInt32(DataArry[4]);
logrecord.strRuleName = DataArry[5].ToString();
logrecord.strValue = DataArry[6].ToString();
logrecord.iStatus = Convert.ToInt32(DataArry[7]);
logrecord.iAction = Convert.ToInt32(DataArry[8]);
logrecord.strActionParam = DataArry[9].ToString();
logrecord.strReserve1 = DataArry[10].ToString();
logrecord.strReserve2 = DataArry[11].ToString();
m_RecordQueue.Enqueue(logrecord);
}
}
catch(Exception e)
{
m_SysLog.WriteToSysLog("Interface:监控日志文件读取错误,原因:{0}", e.Message+e.StackTrace);
}
finally
{
if(m_fstream!=null)
m_fstream.Close();
if(m_logReader!=null)
m_logReader.Close();
}
try
{
File.Delete(m_strFileName);
}
catch(Exception)
{
}
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 + -