Question Problem with setting Timer in Windows Service

Hitz

Member
Joined
Jul 22, 2011
Messages
9
Programming Experience
Beginner
Hi,
I added timer control from toolbox->components->timer and when i saw service1.designer.vd it was windows form timer that was present in the service.

What i need is System.Timers timer .How to use the timer control of System.Timers namespace.

I saw some sample code in forums and tried to run,but the timer was not at all triggered,

here is the code:

Service1.vb:
Imports System.IO
Public Class MyNewService1

Protected Overrides Sub OnStart(ByVal args() As String)
Timer1.Interval = 3000
Timer1.Enabled = True
FileOpen(1, "C:\Users\coop2\Desktop\sample.txt", OpenMode.Append)
Print(1, "Start")
FileClose()
End Sub

Protected Overrides Sub OnStop()
FileOpen(1, "C:\Users\coop2\Desktop\sample.txt", OpenMode.Append)
Print(1, "Stop")
FileClose()
End Sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs)
Timer1.Enabled = False
FileOpen(1, "C:\Users\coop2\Desktop\sample.txt", OpenMode.Append)
Print(1, "Tick")
FileClose()
Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick_1(sender As System.Object, e As System.EventArgs)
Timer1.Enabled = False
FileOpen(1, "C:\sample.txt", OpenMode.Append)
Print(1, "Tick")
FileClose()
Timer1.Enabled = True
End Sub
End Class

service1.designer.vb:

Imports System.ServiceProcess

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class MyNewService1
Inherits System.ServiceProcess.ServiceBase

'UserService overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

' The main entry point for the process
<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New MyNewService1}

System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
'
'MyNewService1
'
Me.ServiceName = "MyNewService1"

End Sub
Friend WithEvents Timer1 As System.Windows.Forms.Timer

End Class


Can someone help me what to change in this code to make it working.

Thank You.
 
You can add the Timers.Timer to the toolbox as well, 'choose toolbox items' and select it from the .Net list of components. The usage is basically the same as for the Forms.Timer. Timer Class (System.Timers)
 
Hi,
i changed that code but when i am starting the service i am getting an error saying "Service has been started and stopped.This is because no other services are using this service."

It was executing only for one time and it was stopping.

How can i fix this??

This is my code:

Protected Overrides Sub OnStart(ByVal args() As String)
AddHandler timer.Elapsed, AddressOf Me.OnElapsedTime
timer.Interval = 10000
timer.Enabled = True
AddToFile("starting service")
run_timer()
End Sub
Private Sub OnElapsedTime(ByVal source As Object, ByVal e As ElapsedEventArgs)
run_timer() //it is a function which returns nothing and performs some operation on database,
AddToFile("starting service")
End Sub
Protected Overrides Sub OnStop()
timer.Enabled = False
AddToFile("stopping service")
End Sub
 
Back
Top