Microsoft KB Archive/820639

= The Windows Forms Timer event is not raised in a Windows service =

Article ID: 820639

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





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:  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. Switch to the Windows Service project. In the Service1 Designer window, click the Timer1 control. Press the DELETE key to remove the Timer1 control from the Designer window. On the toolbar, click the Components tab. From the toolbar, drag a Timer control to the Designer window.</li> Double-click the Timer1 control on the Designer window to view the Code window.</li> Move the code in the Tick event handler of the Timer1 control to the Elapsed event handler of the Timer1 control.</li> Remove the Tick event handler.</li> On the Build menu, click Build Solution.</li> Run the following command in the Command window:

installutil WindowsService1.exe

</li> On the Administrative Tools menu, click Services.</li> Right-click Service1, and then click Start.</li> Wait several seconds, right-click Service1, and then click Stop.</li> Open the C:\sample.txt file, and then notice the text.

The text Tick is displayed with Start and Stop.</li></ol>

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce the Issue
<ol> Start Microsoft Visual Studio .NET.</li> Open a new Windows Service project by using Microsoft Visual Basic .NET or Microsoft Visual C# .NET.

By default, Service1 is created.</li> In the toolbox, click the Windows Forms tab.</li> Drag a Timer control from the toolbox to the Service1 Designer window.</li> On the Designer window, double-click the Timer1 control.

The Service1 Code window is displayed.</li>  If you are using Visual C# .NET, add the following statement to the beginning of the code: using System.IO; </li>  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, &quot;C:\sample.txt&quot;, OpenMode.Append) 'Print text to the &quot;C:/sample.txt file. Print(1, &quot;Start&quot;) 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(@&quot;C:\sample.txt&quot;); writer.WriteLine(&quot;Start&quot;); writer.Close; </li> <li> 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, &quot;C:\sample.txt&quot;, OpenMode.Append) 'Print the text 'Stop' to the C:\sample.txt file. Print(1, &quot;Stop&quot;) FileClose Visual C# .NET Code //Append the text Stop to the C:\sample.txt file. StreamWriter writer =File.AppendText(@&quot;C:\sample.txt&quot;); writer.WriteLine(&quot;Stop&quot;); writer.Close; </li> <li> 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, &quot;C:\sample.txt&quot;, OpenMode.Append) 'Print the text 'Tick' to the C:\sample.txt file. Print(1, &quot;Tick&quot;) 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(@&quot;C:\sample.txt&quot;); writer.WriteLine(&quot;Tick&quot;); writer.Close; timer1.Enabled =true; </li> <li>On the View menu, click Designer.</li> <li>Right-click Designer, and then click Add Installer.

By default, ServiceInstaller1 and ServiceProcessInstaller1 are created.</li> <li>Right-click ServiceInstaller1, and then click Properties.</li> <li>Set the DisplayName property to Service1.

Note It is a good idea to set the ServiceName property to Service1 .</li> <li>Right-click ServiceProcessInstaller1, and then click Properties.</li> <li>In the ServiceProcessInstaller1 Properties window, set the Account property to LocalSystem .</li> <li>On the Build menu, click Build Solution.</li> <li>Click Start, and then click Run.</li> <li>In the Run window, type cmd in the Open box, and then press the ENTER key.</li> <li> 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. </li> <li>After your installation is complete, click Administrative Tools in Control Panel, and then click Services.</li> <li>In the Services window, right-click Service1, and then click Start.</li> <li>Wait several seconds, right-click Service1, and then click Stop.</li> <li>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.</li></ol>

<div class="references_section">