473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Windows Service Timer Woes

I've got a Windows Service class, and I put a System.Threading.Timer, and
I've coded it as shown below. However, when I install the service and then
start it in MMC, I get a peculiar message:

The MyService service on Local Computer started and then stopped. Some
services stop automatically if they ahve no work to do, for example, the
Perforamnce Logs and Alert service.

I tried switching to a System.Threading.Timer and that didn't work either.

Can someone please tell me what I'm doing wrong here?

Thanks

Imports System.ServiceProcess
Imports System.Timers

Public Class Service
Inherits System.ServiceProcess.ServiceBase

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call
' Create the delegate that invokes methods for the timer.
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

' The main entry point for the process
<MTAThread()> _
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
Service}

System.ServiceProcess.ServiceBase.Run(ServicesToRu n)

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.
Friend WithEvents timer As System.Timers.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.timer = New System.Timers.Timer
CType(Me.timer,
System.ComponentModel.ISupportInitialize).BeginIni t()
'
'timer
'
Me.timer.Enabled = True
'
'Service
'
Me.ServiceName = "DrugScreen.com Batch Processing Service"
CType(Me.timer, System.ComponentModel.ISupportInitialize).EndInit( )

End Sub

#End Region

Protected Overrides Sub OnStart(ByVal args() As String)

Const LogName As String = "SECON Batch Processor"
Const SourceName As String = "SECON Batch Processor"
Const FiveMinutes As Integer = 300000

If Not EventLog.SourceExists(SourceName) Then
EventLog.CreateEventSource(SourceName, LogName)
End If

EventLog.WriteEntry("Service started.",
EventLogEntryType.Information)

' Immediately process any pending work
BatchProcessor.ProcessBatch()

timer.Interval = FiveMinutes
timer.Enabled = True
timer.Start()

End Sub

Protected Overrides Sub OnStop()
timer.Stop()
timer.Dispose()
timer = Nothing
End Sub

Protected Overrides Sub OnPause()
timer.Enabled = False
EventLog.WriteEntry("Service paused.",
EventLogEntryType.Information)
End Sub

Protected Overrides Sub OnContinue()
timer.Enabled = True
EventLog.WriteEntry("Service resumed.",
EventLogEntryType.Information)
End Sub

Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles timer.Elapsed
BatchProcessor.ProcessBatch()
End Sub

End Class


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 21 '05 #1
2 4025
In article <42**********@127.0.0.1>, John David Thornton wrote:
I've got a Windows Service class, and I put a System.Threading.Timer, and
I've coded it as shown below. However, when I install the service and then
start it in MMC, I get a peculiar message:

The MyService service on Local Computer started and then stopped. Some
services stop automatically if they ahve no work to do, for example, the
Perforamnce Logs and Alert service.

I tried switching to a System.Threading.Timer and that didn't work either.

Can someone please tell me what I'm doing wrong here?

Thanks

Imports System.ServiceProcess
Imports System.Timers

Public Class Service
Inherits System.ServiceProcess.ServiceBase

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call
' Create the delegate that invokes methods for the timer.
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

' The main entry point for the process
<MTAThread()> _
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
Service}

System.ServiceProcess.ServiceBase.Run(ServicesToRu n)

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.
Friend WithEvents timer As System.Timers.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.timer = New System.Timers.Timer
CType(Me.timer,
System.ComponentModel.ISupportInitialize).BeginIni t()
'
'timer
'
Me.timer.Enabled = True
'
'Service
'
Me.ServiceName = "DrugScreen.com Batch Processing Service"
CType(Me.timer, System.ComponentModel.ISupportInitialize).EndInit( )

End Sub

#End Region

Protected Overrides Sub OnStart(ByVal args() As String)

Const LogName As String = "SECON Batch Processor"
Const SourceName As String = "SECON Batch Processor"
Const FiveMinutes As Integer = 300000

If Not EventLog.SourceExists(SourceName) Then
EventLog.CreateEventSource(SourceName, LogName)
End If

EventLog.WriteEntry("Service started.",
EventLogEntryType.Information)

' Immediately process any pending work
BatchProcessor.ProcessBatch()

timer.Interval = FiveMinutes
timer.Enabled = True
timer.Start()

End Sub

Protected Overrides Sub OnStop()
timer.Stop()
timer.Dispose()
timer = Nothing
End Sub

Protected Overrides Sub OnPause()
timer.Enabled = False
EventLog.WriteEntry("Service paused.",
EventLogEntryType.Information)
End Sub

Protected Overrides Sub OnContinue()
timer.Enabled = True
EventLog.WriteEntry("Service resumed.",
EventLogEntryType.Information)
End Sub

Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles timer.Elapsed
BatchProcessor.ProcessBatch()
End Sub

End Class


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com


John... I think you need to redesign a bit. I expect that, just
calling the start is not enough to keep the process alive.

What I would do is start a worker thread from the onstart. Then you
could handle the timer from there. It would look something like:

Private waitObject As New ManualResetEvent (False)

Protected Overrides Sub OnStart(ByVal args() As String)
Dim worker As New Thread (AddressOf Me.WorkProc)
worker.Start ()
End Sub

Private Sub WorkProc ()
' imediately process any initial work work
BatchProcessor.ProcessBatch()

' set up the timer
timer.Interval = FiveMinutes
timer.AutoReset = False
timer.Enabled = False

do while (processing)
timer.Start ()
waitObject.WaitOne ()
BatchProcessor.ProcessBatch()
waitObject.Reset ()
loop

End Sub

Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As
timer.Enabled
waitObject.Set ()
End Sub

Anyway, that is a basic, obviously air-code example, but hopefully that
will get you started.

--
Tom Shelton [MVP]
Nov 21 '05 #2
Thank you very much! That solution worked beautifully!

"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:e6**************@TK2MSFTNGP14.phx.gbl...
In article <42**********@127.0.0.1>, John David Thornton wrote:
I've got a Windows Service class, and I put a System.Threading.Timer, and
I've coded it as shown below. However, when I install the service and
then
start it in MMC, I get a peculiar message:

The MyService service on Local Computer started and then stopped. Some
services stop automatically if they ahve no work to do, for example, the
Perforamnce Logs and Alert service.

I tried switching to a System.Threading.Timer and that didn't work
either.

Can someone please tell me what I'm doing wrong here?

Thanks

Imports System.ServiceProcess
Imports System.Timers

Public Class Service
Inherits System.ServiceProcess.ServiceBase

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call
' Create the delegate that invokes methods for the timer.
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

' The main entry point for the process
<MTAThread()> _
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
Service}

System.ServiceProcess.ServiceBase.Run(ServicesToRu n)

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.
Friend WithEvents timer As System.Timers.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.timer = New System.Timers.Timer
CType(Me.timer,
System.ComponentModel.ISupportInitialize).BeginIni t()
'
'timer
'
Me.timer.Enabled = True
'
'Service
'
Me.ServiceName = "DrugScreen.com Batch Processing Service"
CType(Me.timer,
System.ComponentModel.ISupportInitialize).EndInit( )

End Sub

#End Region

Protected Overrides Sub OnStart(ByVal args() As String)

Const LogName As String = "SECON Batch Processor"
Const SourceName As String = "SECON Batch Processor"
Const FiveMinutes As Integer = 300000

If Not EventLog.SourceExists(SourceName) Then
EventLog.CreateEventSource(SourceName, LogName)
End If

EventLog.WriteEntry("Service started.",
EventLogEntryType.Information)

' Immediately process any pending work
BatchProcessor.ProcessBatch()

timer.Interval = FiveMinutes
timer.Enabled = True
timer.Start()

End Sub

Protected Overrides Sub OnStop()
timer.Stop()
timer.Dispose()
timer = Nothing
End Sub

Protected Overrides Sub OnPause()
timer.Enabled = False
EventLog.WriteEntry("Service paused.",
EventLogEntryType.Information)
End Sub

Protected Overrides Sub OnContinue()
timer.Enabled = True
EventLog.WriteEntry("Service resumed.",
EventLogEntryType.Information)
End Sub

Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles timer.Elapsed
BatchProcessor.ProcessBatch()
End Sub

End Class


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com


John... I think you need to redesign a bit. I expect that, just
calling the start is not enough to keep the process alive.

What I would do is start a worker thread from the onstart. Then you
could handle the timer from there. It would look something like:

Private waitObject As New ManualResetEvent (False)

Protected Overrides Sub OnStart(ByVal args() As String)
Dim worker As New Thread (AddressOf Me.WorkProc)
worker.Start ()
End Sub

Private Sub WorkProc ()
' imediately process any initial work work
BatchProcessor.ProcessBatch()

' set up the timer
timer.Interval = FiveMinutes
timer.AutoReset = False
timer.Enabled = False

do while (processing)
timer.Start ()
waitObject.WaitOne ()
BatchProcessor.ProcessBatch()
waitObject.Reset ()
loop

End Sub

Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As
timer.Enabled
waitObject.Set ()
End Sub

Anyway, that is a basic, obviously air-code example, but hopefully that
will get you started.

--
Tom Shelton [MVP]

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: andrewcw | last post by:
I am trying to do a windows service with C#. I am using as a base the VB.NET article in VS, but I thing the WITHEVENTS timer notation is a delegate. Can anyone provide sample code & anh hints. ...
2
by: Jesper Stocholm | last post by:
I have created a simple service which just copies a fil to a new file with a new name on certain intervals (the service implements a timer). I have no problems installing the service and the...
5
by: Dhilip Kumar | last post by:
Hi all, I have developed a windows service using the windows service project template in VS.NET. I have used three controls in the service, a timer, performance counter and a message queue...
3
by: Nathan Kovac | last post by:
I have a feeling I am missing something simple, but I just can't find it. Perhaps someone can give me a lead on where to look. I will describe the issue then post my code to the web service. My...
3
by: Yves Royer | last post by:
Hi all, I have a little question about Windows Services. For my app i need 3 different Windows Services, each with its own functionality. I'm trying to make a test service to see what happens...
4
by: Groundskeeper | last post by:
I can't seem to get a custom UnhandledException handler working for a Windows Service I'm writing in VB.NET. I've read the MSDN and tried various different placements of the AddHandler call, to no...
5
by: Tom | last post by:
Using multiple System.Timers.Timer objects in a Windows Service for performing multi-thread activities in a periodic fashion. Timers are AutoReset=false, to only have a single timer execution...
4
by: Lemune | last post by:
Hello everyone. I'm using vb 2005. I'm creating program that run as service on windows. And in my program I need to use timer, so I'm using timer object from component. I try my source code on...
5
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.