The Windows Forms Timer event is not raised in a Windows service (820639)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

SYMPTOMS

You add a Microsoft Windows Forms timer component to your Windows service. You enable the timer, and then you set the interval for the timer to raise an event. You install the Windows service on your computer, and then you start the service. The Timer event in the Windows service is not raised.

Note The Windows Forms timer component is located in the System.Windows.Forms namespace.

CAUSE

The Windows Forms timer component is designed for a Windows Forms environment. The Windows Forms timer component is not designed for a server environment. Therefore, the timer might not raise events if you use it in a Windows service.

RESOLUTION

To resolve this issue, use server timers from the System.Timers namespace instead of Windows Forms timers from the System.Windows.Forms namespace. To do this, follow these steps:
  1. Run the following command in the Command window to remove the Service1 class:

    installutil /u WindowsService1.exe

    Note WindowsService1.exe is located in the bin folder under your project folder. Add the path of your WindowsService1.exe.
  2. Switch to the Windows Service project.
  3. In the Service1 Designer window, click the Timer1 control.
  4. Press the DELETE key to remove the Timer1 control from the Designer window.
  5. On the toolbar, click the Components tab.
  6. From the toolbar, drag a Timer control to the Designer window.
  7. Double-click the Timer1 control on the Designer window to view the Code window.
  8. Move the code in the Tick event handler of the Timer1 control to the Elapsed event handler of the Timer1 control.
  9. Remove the Tick event handler.
  10. On the Build menu, click Build Solution.
  11. Run the following command in the Command window:

    installutil WindowsService1.exe

  12. On the Administrative Tools menu, click Services.
  13. Right-click Service1, and then click Start.
  14. Wait several seconds, right-click Service1, and then click Stop.
  15. Open the C:\sample.txt file, and then notice the text.

    The text Tick is displayed with Start and Stop.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Issue

  1. Start Microsoft Visual Studio .NET.
  2. Open a new Windows Service project by using Microsoft Visual Basic .NET or Microsoft Visual C# .NET.

    By default, Service1 is created.
  3. In the toolbox, click the Windows Forms tab.
  4. Drag a Timer control from the toolbox to the Service1 Designer window.
  5. On the Designer window, double-click the Timer1 control.

    The Service1 Code window is displayed.
  6. If you are using Visual C# .NET, add the following statement to the beginning of the code:
    using System.IO;
  7. Add the following code to the OnStart procedure of the Service1 class.

    Visual Basic .NET Code
    'Set the interval of the timer to 3 seconds.
    Timer1.Interval = 3000
    Timer1.Enabled = True
    'Open the sample.txt file in append mode.
    FileOpen(1, "C:\sample.txt", OpenMode.Append)
    'Print text to the "C:/sample.txt file.
    Print(1, "Start")
    FileClose()
    Visual C# .NET Code
    //Set the interval of timer to 3 seconds.
    timer1.Interval =3000;
    //Enable the timer.
    timer1.Enabled =true;
    //Append the text to the sample file.
    StreamWriter writer =File.AppendText(@"C:\sample.txt");
    writer.WriteLine("Start");           
    writer.Close();
  8. Add the following code to the OnStop procedure of the Service1 class.

    Visual Basic .NET Code
    'Open the C:\sample.txt file in append mode.
    FileOpen(1, "C:\sample.txt", OpenMode.Append)
    'Print the text 'Stop' to the C:\sample.txt file.
    Print(1, "Stop")
    FileClose()
    Visual C# .NET Code
    //Append the text Stop to the C:\sample.txt file.
    StreamWriter writer =File.AppendText(@"C:\sample.txt");
    writer.WriteLine("Stop");
    writer.Close();
  9. Add the following code to the Tick event of the Timer1 component.

    Visual Basic .NET Code
     'Set the enabled property to false.
    Timer1.Enabled = False
    'Open the C:\sample.txt file in append mode.
    FileOpen(1, "C:\sample.txt", OpenMode.Append)
    'Print the text 'Tick' to the C:\sample.txt file.
    Print(1, "Tick")
    FileClose()
    'Enable the timer.
    Timer1.Enabled = True
    Visual C# .NET Code
    //Set the enable property to false.
    timer1.Enabled =false;
    //Append the text Tick to the C:\sample.txt file.
    StreamWriter writer =File.AppendText(@"C:\sample.txt");
    writer.WriteLine("Tick");
    writer.Close();
    timer1.Enabled =true;
  10. On the View menu, click Designer.
  11. Right-click Designer, and then click Add Installer.

    By default, ServiceInstaller1 and ServiceProcessInstaller1 are created.
  12. Right-click ServiceInstaller1, and then click Properties.
  13. Set the DisplayName property to Service1.

    Note It is a good idea to set the ServiceName property to Service1.
  14. Right-click ServiceProcessInstaller1, and then click Properties.
  15. In the ServiceProcessInstaller1 Properties window, set the Account property to LocalSystem.
  16. On the Build menu, click Build Solution.
  17. Click Start, and then click Run.
  18. In the Run window, type cmd in the Open box, and then press the ENTER key.
  19. At the command prompt, run the following command:
    installutil  WindowsService1.exe
    Note Your Service1 service is not installed if you are using any other service with the same name.
  20. After your installation is complete, click Administrative Tools in Control Panel, and then click Services.
  21. In the Services window, right-click Service1, and then click Start.
  22. Wait several seconds, right-click Service1, and then click Stop.
  23. Open the C:\sample.txt file.

    The text StartStop is displayed. The Timer1Tick event is not raised, and the Tick text is not printed to the C:\sample.txt file.

REFERENCES

For more information about Windows Service applications, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbvs2005doesnotapply kbvs2005swept kbServiceProcess kbCtrl kbControl kbEvent kbTimer kbWindowsForms kbService kbprb KB820639 kbAudDeveloper