Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to add a timer component/control to a Windows Service project

Status
Not open for further replies.

ericnet

Programmer
Mar 29, 2006
106
0
0
I recently downloaded SharpDevelop 1.1 in order to create a Windows Service to collect, monitor and store into SQL database performance counter values of my server. Now I am doing my first steps creating a Windows Service example, but I don’ t know how to add a timer component/control to my recently created Windows Service. Somebody knows which are the steps in SharpDevelop design window to add that timer component to a Windows Service project? Or how to do it in Visual Studio so that I can deduce how to do it in SharpDevelop?

I am following the instructions of this article to create that Windows Service example, and it is explained using Visual Studio. The part of the instructions that explains how to add the timer control says:
..Open Visual Studio .NET and create a new Windows service project, which we shall call "MyService". Click OK.
Add a timer control from the toolbar in the Components tab. In the properties window of Timer1, change the interval property to 10000, which is 10 seconds...

Another article I found, says more or less the same:
Well, start off by creating your own Windows Service project in Visual Studio .NET.....One of the most common procedures in designing your Windows service is to add a Timer component and have it fire your service code every x seconds or minutes. How can you get this to work? While still in Design mode, open the Components tab of the toolbox and drag and drop a Timer component onto your service. Change its Interval property to the number of milliseconds between times you want your code to run. (Remember, 1,000 milliseconds equals one second. If you want your code to run every ten minutes, for example, set this to 60000.)...

Here is the complete article:
Which are the steps to follow (with either Visual Studio or SharpDevelop) to create that Timer component? I successfully created the Windows Service, and now I see the vb code file created by SharpDevelop, but I don’ t absolutely find how to add that Timer..

Thank you
 
You can declare a timer as a class-level variable:

Private WithEvents TmrStartUp As System.Timers.Timer

Me.TmrStartUp.Interval = ((1000 * 60) * 60) * 24
Me.TmrStartUp.AutoReset = True
Me.TmrStartUp.Enabled = True
 
There are actually three timer objects in the .net framework. Two of them depend on the existence of a message pump (they derive from Component), which a service will probably not have. So, use the third one:

[tt]System.Threading.Timer[/tt]

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
The timer components showing in the toolbox (both in the Component and All Windows Forms sections) are BOTH windows forms timers. This forms timer component doesn't work in a service, you need the system timer as Chip mentioned.

You can add this to your tool box by right clicking on the toolbox and select "Choose items" from the popup menu. Then filter by timer to see the available timers. Make sure the timer in the Systems.timer namespace is selected. You might also want to rename it once it's in the toolbox to differentiate it from the form timer.
 
Oh yes, don't worry. I solved the problem just some time before I saw the answers, and yes, the solution was add the timer manually using System.Threading.Timer class

Thank you for your answers!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top