windows简单服务,为什么我的定时器里的代码无效?
代码如下:
public FileResetService()
{
InitializeComponent();
TimeDo();
}
protected override void OnStart(string[] args)
{
//TimeDo();
}
/// <summary>
/// /重新启动
/// </summary>
public void Reset()
{
//关闭进程
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("FileResultColection");
foreach (System.Diagnostics.Process p in process)
{
p.Kill();
}
//启动程序
System.Diagnostics.Process.Start(@"F:\Work\快分系统\VS2008新仪器数据采集\DataAcquisitionSolution\FileResultColection\bin\FileResultColection.exe");
}
public void TimeDo()
{
//System.Timers.Timer aTimer = new System.Timers.Timer();
//aTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimeEvent);
timer1.Enabled = true;
timer1.Interval = 1000;
}
///// <summary>
///// 定时器触发事件
///// </summary>
///// <param name="source"></param>
///// <param name="e"></param>
//private void TimeEvent(object source, System.Timers.ElapsedEventArgs e)
//{
//}
public void WirteLogWithName(string msg, string filePath)
{
for (int i = 0; i < 5; i++)
{
try
{
System.IO.File.AppendAllText(filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Info>" + msg + System.Environment.NewLine, System.Text.Encoding.UTF8);
break;
}
catch (
System.Exception e)
{
System.Threading.Thread.Sleep(500);
continue;
}
}
}
protected override void OnStop()
{
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
int intHour = e.SignalTime.Hour;
int intMinute = e.SignalTime.Minute;
int intSecond = e.SignalTime.Second;
int iHour = 12;
int iMinute = 47;
int iSecond = 00;
// 设置重启启动的的时间
if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)
{
Reset();
}
iMinute = 48;
if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)
{
Reset();
}
iMinute = 49;
if (intHour == iHour && intMinute == iMinute && intSecond == iSecond)