我正在编写一个Windows服务应用程序,我使用计时器控件。在我的Windows服务的OnStart()事件中,我启动计时器,我希望每隔一分钟调用一次StartTimer(),但没有任何事情发生。
这里有什么问题?
感谢。
myWinService.cs :::
protected override void OnStart(string[] args)
{
timer1.Interval=60000;
timer1.Start();
}
private void StartTimer()
{
FileStream fs = new FileStream(@"c:\temp\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(Environment.UserName.ToString()+tik.ToString());
m_streamWriter.Flush();
}
private void timer1_Tick(object sender, EventArgs e)
{
tik++;
StartTimer();
}
答案 0 :(得分:5)
如@Gunner的评论中所述,您还没有联系到Timer.Tick
事件。
在OnStart
方法中,您需要使用timer1_Tick
事件注册Tick
方法:
timer1.Tick += new EventHandler(timer1_Tick);