赢得服务应用问题

时间:2011-03-05 07:32:26

标签: c# windows-services

我正在编写一个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();
        }

1 个答案:

答案 0 :(得分:5)

如@Gunner的评论中所述,您还没有联系到Timer.Tick事件。

OnStart方法中,您需要使用timer1_Tick事件注册Tick方法:

timer1.Tick += new EventHandler(timer1_Tick);