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.
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.