Problem with win service application

I am writing a Windows application and I am using a timer. In the OnStart () event of my Windows service, I start a timer, and I want StartTimer () to be called every minute, but nothing happens.

What is wrong here?

thank.

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();
        }
+3
source share
1 answer

As noted in a comment by @Gunner, you did not hook the event Timer.Tick.

In your method OnStartyou need to register a method timer1_Tickwith an event Tick:

timer1.Tick += new EventHandler(timer1_Tick); 
+5
source

Source: https://habr.com/ru/post/1796453/


All Articles