I guess most experts here will be familiar with the ntsvc.ocx and its many sample programs. I have created a service with this which runs great. The *only* problem is that the service needs to tidy up some stuff on shutdown.
For the life of me, I cannot get the shutdown event to be handled. I have attached a sample app which runs off a form with the ntsvc.ocx, a timer and a status bar control. The only modifications that I've made to the sample code are a series of beep statements. The service happily beeps once for start and twice for stop. However it does not make a sound when the machine reboots. I'd be really happy if somebody can hack it so it gives three beeps just before Windows shuts down.
I have tried adding a NTService1_Shutdown() handler, Form_UnloadQuery() and Form_Unload() subs but I cannot get the service to run code on shutdown.
This is bugging me and I really like to fix this badly, hence the high point value. Any sample code that runs something on a reboot is fine, as I need to port it back to my main project anyway. My real code doesn't have .interactive=true and only outputs to the app log.
Thanks
===================== sample code ===============
Option Explicit
Private Sub Form_Load()
On Error GoTo Err_Load
Dim strDisplayName As String
Dim bStarted As Boolean
strDisplayName = NTService1.DisplayName
StatusBar.Panels(1).Text = "Loading"
If Command = "-install" Then
' enable interaction with desktop
NTService1.Interactive = True
If NTService1.Install Then
Call NTService1.SaveSetting("Parameters", "TimerInterval", "1000")
MsgBox strDisplayName & " installed successfully"
Else
MsgBox strDisplayName & " failed to install"
End If
End
ElseIf Command = "-uninstall" Then
If NTService1.Uninstall Then
MsgBox strDisplayName & " uninstalled successfully"
Else
MsgBox strDisplayName & " failed to uninstall"
End If
End
ElseIf Command = "-debug" Then
NTService1.Debug = True
ElseIf Command <> "" Then
MsgBox "Invalid command option"
End
End If
Timer.Interval = CInt(NTService1.GetSetting("Parameters", "TimerInterval", "2000"))
' enable Pause/Continue. Must be set before StartService
' is called or in design mode
NTService1.ControlsAccepted = svcCtrlPauseContinue + svcCtrlShutdown
' connect service to Windows NT services controller
NTService1.StartService
Err_Load:
Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description)
End Sub
Private Sub NTService1_Continue(Success As Boolean)
On Error GoTo Err_Continue
Timer.Enabled = True
StatusBar.Panels(1).Text = "Running"
Success = True
Call NTService1.LogEvent(svcEventInformation, svcMessageInfo, "Service continued")
Err_Continue:
Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description)
End Sub
Private Sub NTService1_Control(ByVal lngEvent As Long)
On Error GoTo Err_Control
Beep
Beep
Beep
Err_Control:
Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description)
End Sub
Private Sub NTService1_Pause(Success As Boolean)
On Error GoTo Err_Pause
Timer.Enabled = False
StatusBar.Panels(1).Text = "Paused"
Call NTService1.LogEvent(svcEventError, svcMessageError, "Service paused")
Success = True
Err_Pause:
Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description)
End Sub
Private Sub NTService1_Start(Success As Boolean)
On Error GoTo Err_Start
Beep
StatusBar.Panels(1).Text = "Running"
Success = True
Err_Start:
Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description)
End Sub
Private Sub NTService1_Stop()
On Error GoTo Err_Stop
Beep
Beep
StatusBar.Panels(1).Text = "Stopped"
Unload Me
Err_Stop:
Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description)
End Sub
Private Sub Timer_Timer()
On Error GoTo Err_Timer
StatusBar.Panels(2).Text = Format(Now(), "hh:mm:ss")
Err_Timer:
Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description)
End Sub
by: BiffoPosted on 2003-07-17 at 12:23:05ID: 8945733
What about creating a task to run at a certain time independent of your app?