What account does the service run under ?, it should be set to run under the
machine account or administrator. You can check this in control
panel/services.
--
OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me
Time flies when you don't know what you're doing
"Dirk Herman via .NET 247" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
Hi,
I am trying to write a service in VB.Net to prevent our users to shut down
their PC. Whenever a shutdown is initiated, the shutdown should be replaced
by a reboot. I used the following code, which starts a thread upon service
start. This thread runs the sub Listen(), which adds a handler for the
SessionEnding event. This code works fine when I use it in a "standard"
application which contains a form and is started in user context, but when I
use a service, the AddHandler line generates an error "Failed to create
system events window thread". Does anyone have any idea about a possible
solution to this problem?
Imports System.ServiceProcess
Imports Microsoft.Win32
Imports System.Threading
Public Class ForceRbt
Inherits System.ServiceProcess.ServiceBase
+ #Region " Component Designer generated code "
Protected Overrides Sub OnStart(ByVal args() As String)
Dim oListen As New Listener
Dim t As New Thread(AddressOf oListen.Listen)
EventLogger.WriteToEventLog("starting thread")
t.Start()
End Sub
End Class
Public Class Listener
Public Sub Listen()
EventLogger.WriteToEventLog("inside thread")
Try
AddHandler Microsoft.Win32.SystemEvents.SessionEnding, _
New SessionEndingEventHandler(AddressOf SessionIsEnding)
Catch ex As Exception
EventLogger.WriteToEventLog(ex.Message)
End Try
End Sub
Public Sub SessionIsEnding(ByVal sender As Object, ByVal e As
SessionEndingEventArgs)
Select Case e.Reason
Case SessionEndReasons.SystemShutdown
EventLogger.WriteToEventLog("shutdown detected :
Cancelling")
e.Cancel = True
EventLogger.WriteToEventLog("shutdown detected : starting
reboot")
Case SessionEndReasons.Logoff
EventLogger.WriteToEventLog("Logoff detected")
e.Cancel = True
ForceRebootService.modToken.Restart()
End Select
End Sub
End Class
Public Class EventLogger
Public Shared Sub WriteToEventLog(ByVal sMessage As String)
If Not EventLog.SourceExists("TestRbt") Then
EventLog.CreateEventSource("TestRbt", "Application")
End If
Dim oLogEntry As New EventLog
oLogEntry.WriteEntry("TestRbt", sMessage)
End Sub
End Class
--------------------------------
From: Dirk Herman
-----------------------
Posted by a user from .NET 247 (
http://www.dotnet247.com/)
<Id>/63Y53i4306GW7IlQWZhwA==</Id>