PC Review


Reply
Thread Tools Rate Thread

Re: SessionEndingEventHandler problem in Service

 
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      16th Sep 2004
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>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP! VB.net Service error: "Could not start service on local computer. Error 1053: The service did not respond to the start or control request in a timely fashion." mrwoopey Microsoft Dot NET Framework 4 21st Aug 2008 06:27 PM
Unable to retrieve service name of VB.Net service after service st =?Utf-8?B?VG9t?= Microsoft Dot NET Framework 0 20th Jan 2006 10:01 AM
The <service> service hung on starting. / Service started successfully. =?Utf-8?B?cm9iX3Nhcw==?= Microsoft Dot NET 0 12th Feb 2004 11:21 PM
ICS Problem: Error 1060 The specific service does not exist as an installed service !! How to fix this! Joe90 Windows XP Networking 0 1st Feb 2004 11:02 PM
Can anyone tell me why Invoking from a service doesn't work please? - Service Problem.txt (0/1) Mike Microsoft VB .NET 0 21st Dec 2003 07:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:45 PM.