Re: SessionEndingEventHandler problem in Service

Discussion in 'Microsoft VB .NET' started by One Handed Man \( OHM - Terry Burns \), Sep 16, 2004.

  1. 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" <> wrote in message
    news:...
    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>
     
    One Handed Man \( OHM - Terry Burns \), Sep 16, 2004
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Genojoe
    Replies:
    2
    Views:
    336
  2. SAVAGE
    Replies:
    0
    Views:
    239
    SAVAGE
    Dec 21, 2003
  3. Mike
    Replies:
    0
    Views:
    213
  4. Zacknafein
    Replies:
    0
    Views:
    248
    Zacknafein
    Dec 23, 2003
  5. acool
    Replies:
    1
    Views:
    818
    Herfried K. Wagner [MVP]
    May 17, 2004
Loading...

Share This Page