Window Service and SystemEvents.SessionEnding

I would like to build a Windows service with the SessionEnding event but get the following error when I try to start the service using the following code. What am I doing wrong if this is possible.

Protected Overrides Sub OnStart(ByVal args() As String)
AddHandler
SystemEvents.SessionEnding, AddressOf SystemEvents_OnSessionEnding
End Sub

Public Sub SystemEvents_OnSessionEnding(ByVal sender As Object, ByVal e As SessionEndingEventArgs)
oXML.LoadXml("<Tests><Test>Sinking event</Test></Tests>")
oXML.Save("C:\test.xml")
End Sub

Failed to create system events window thread.
at Microsoft.Win32.SystemEvents.EnsureSystemEvents(Boolean requireHandle, Boolean throwOnRefusal)
at Microsoft.Win32.SystemEvents.add_SessionEnding(SessionEndingEventHandler value)
at FontManagement.Service1.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)


Thanks

Mark Baird



Answer this question

Window Service and SystemEvents.SessionEnding

  • Sasikumardr

    Hi Mark,

    I think you're running into the same problem I had awhile back where I was trying to create a file from a windows service. Make sure you've enabled the service to interact with the local desktop. Just open the service's properties and select the "Log On" tab...there is a checkbox that needs to be checked labelled "allow service to interact with desktop".

    I hope this is what you're looking for.

    Christopher



  • Window Service and SystemEvents.SessionEnding