VB.Net - Windows Service help

~J~

~J~

Soldato
Joined
20 Oct 2003
Posts
7,558
Location
London
Hiya,

Trying to get a Windows service created for a customer, but I've getting stuck at the first hurdle.

The code below SHOULD generate an error saying it can't display a modal window ('cos I'm using MsgBox), but it's not. It doesn't seem as if the timer elapses event is been fired.

Any ideas as to what I'm doing wrong?

Imports System.ServiceProcess
Imports System.Timers

Public Class BatchImport
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

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 BatchImport}
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()
'
'BatchImport
'
Me.ServiceName = "Watsons Batch Import Service"


End Sub

Private WithEvents ServiceTimer As New System.Timers.Timer(2000)

#End Region

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
ServiceTimer.Start()

End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
ServiceTimer.Stop()
End Sub

Private Sub CallSchedule(ByVal sender As Object, ByVal e As ElapsedEventArgs) Handles ServiceTimer.Elapsed
MsgBox("Hit!")
End Sub

End Class
 
LOL - cheers mate, that's definately not it, it's the IDE spitting it out wrong.

There's no error messages, that's the whole point.

If I put the "MsgBox" command in the OnStart event, I get an error saying that a Modal dialog can't be displayed in a window that doesn't allow user interaction. Makes sense.

But when I put 'anything' in the CallSchedule event, MsgBox or something else that I know will cause an error, or even work, then the program never hits it.

So weird.
 
L33 said:
from what I can see, you've nothing that calls CallSchedule, looks like you need a handler such as:
Code:
AddHandler ServiceTimer.Elapsed, AddressOf CallSchedule
ServiceTimer.Interval = whatever you want

Quote right, I don't. But wouldn't this line do the same...

Private Sub CallSchedule(ByVal sender As Object, ByVal e As ElapsedEventArgs) Handles ServiceTimer.Elapsed

?
 
Nar, done nowt.

Heck, I really can't understand why it's not working. I did this years ago, checked the web, MSDN help and even a book and they all say I'm doing it right.

:(

Hmmmm, I bet it's something stupid!! lol.

Cheers for the help though, appreciated.
 
Back
Top Bottom