Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
        }
like image 990
Farna Avatar asked Jul 24 '26 05:07

Farna


1 Answers

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); 
like image 116
Oded Avatar answered Jul 26 '26 21:07

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!