Farna
Farna

Reputation: 1176

win service app problem

I am writing a windows service app and I use the timer control. In OnStart() event of my windows service I start the timer and I want that StartTimer() is called every one minute, but nothing is happening.

What is wrong here?

thanks.

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();
        }

Upvotes: 3

Views: 164

Answers (1)

Oded
Oded

Reputation: 499002

As noted in the comment by @Gunner, you have not hooked up the Timer.Tick event.

In yourOnStart method, you need to register the timer1_Tick method with the Tick event:

timer1.Tick += new EventHandler(timer1_Tick); 

Upvotes: 5

Related Questions