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)
SYMPTOMSYou 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.CAUSEThe 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. RESOLUTIONTo 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.
- Double-click the Timer1 control on the
Designer window to view the Code window.
- Move the code in the Tick event handler of
the Timer1 control to the Elapsed event
handler of the Timer1 control.
- Remove the Tick event handler.
- On the Build menu, click Build
Solution.
- Run the following command in the Command window:
installutil WindowsService1.exe - On the
Administrative Tools menu, click Services.
- Right-click Service1, and then click
Start.
- Wait several seconds, right-click Service1,
and then click Stop.
- 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.REFERENCESFor more information about Windows Service applications,
visit the following Microsoft Developer Network (MSDN) Web site:
Modification Type: | Minor | Last Reviewed: | 2/3/2006 |
---|
Keywords: | kbvs2005doesnotapply kbvs2005swept kbServiceProcess kbCtrl kbControl kbEvent kbTimer kbWindowsForms kbService kbprb KB820639 kbAudDeveloper |
---|
|