posae
asked on
Killing a process when user invokes shutdown or log off - Urgent
Hello,
I have an app that is running upon startup. When the user shuts down the system or logs off, i get the "End Now" message to kill the process.
Is there any registry entry that I can add upon install of the app so that when the user logs off or shuts down, it kills the process?
Thanks Experts
Eric
I have an app that is running upon startup. When the user shuts down the system or logs off, i get the "End Now" message to kill the process.
Is there any registry entry that I can add upon install of the app so that when the user logs off or shuts down, it kills the process?
Thanks Experts
Eric
How about installing the application as a Windows Service?
ASKER
It is a little trickier than that as the program requires username/password that the user enters upon startup at log-on time.
Sorry, slightly short and sweet reply there :)
Is your application a Windows Forms application or a Console application? Converting it to a service isn't that much of a chore really and would probably be better in the long run.
That way, when the computer starts up, your application will startup in the background and, when it shuts down, Windows will terminate it automatically, with no fuss.
That's assuming you have the ability to create a windows service with your version of Visual Studio ;)
Is your application a Windows Forms application or a Console application? Converting it to a service isn't that much of a chore really and would probably be better in the long run.
That way, when the computer starts up, your application will startup in the background and, when it shuts down, Windows will terminate it automatically, with no fuss.
That's assuming you have the ability to create a windows service with your version of Visual Studio ;)
Ah, sorry, posted my 2nd comment before seeing your addition there
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just had a look into Mastoo's solution and he is a god :) I didn't realise that the functionality existed in .NET but, now I do I plan to make full use of it myself.
Here's an example on MSDN on the use of the SessionEnding event. Please though, if you do use this, give Mastoo the accepted solution. I just googled SessionEnding, which isn't an answer from me at all :) Mastoo came up with the idea and deserves the credit
http://msdn2.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx
Here's an example on MSDN on the use of the SessionEnding event. Please though, if you do use this, give Mastoo the accepted solution. I just googled SessionEnding, which isn't an answer from me at all :) Mastoo came up with the idea and deserves the credit
http://msdn2.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx
ASKER
Thanks for your help guys...I have been a little busy up until now but I will get to it ASAP and then go from there. I appreciate your time.
ASKER
For some reason, the program is not trapping the shutdown. Here is my code
Imports Microsoft.Win32
Public Class dsTokProcessorInfo
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon tainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents NotifyIcon1 As System.Windows.Forms.Notif yIcon
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Cont ainer
Dim resources As System.Resources.ResourceM anager = New System.Resources.ResourceM anager(Get Type(dsTok ProcessorI nfo))
Me.NotifyIcon1 = New System.Windows.Forms.Notif yIcon(Me.c omponents)
'
'NotifyIcon1
'
Me.NotifyIcon1.Icon = CType(resources.GetObject( "NotifyIco n1.Icon"), System.Drawing.Icon)
Me.NotifyIcon1.Text = "docusyst Processor"
Me.NotifyIcon1.Visible = True
'
'dsTokProcessorInfo
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 173)
Me.Icon = CType(resources.GetObject( "$this.Ico n"), System.Drawing.Icon)
Me.Name = "dsTokProcessorInfo"
Me.Text = "dsTokProcessorInfo"
Me.TopMost = True
Me.WindowState = System.Windows.Forms.FormW indowState .Minimized
End Sub
#End Region
Private Sub dsTokProcessorInfo_Load(By Val sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler SystemEvents.SessionEnding , AddressOf OnShuttingdown
AddHandler SystemEvents.SessionEnded, AddressOf OnShutdown
Dim endsessionHandler As SessionEndedEventHandler
AddHandler SystemEvents.SessionEnded, endsessionHandler
Me.Hide()
End Sub
Public Shared Sub OnShuttingdown(ByVal sender As Object, ByVal e As SessionEndingEventArgs)
msgbox("Shutting Down")
End Sub
Public Shared Sub OnShutdown(ByVal sender As Object, ByVal e As SessionEndedEventArgs)
msgbox("Shut Down")
End Sub
Imports Microsoft.Win32
Public Class dsTokProcessorInfo
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents NotifyIcon1 As System.Windows.Forms.Notif
<System.Diagnostics.Debugg
Me.components = New System.ComponentModel.Cont
Dim resources As System.Resources.ResourceM
Me.NotifyIcon1 = New System.Windows.Forms.Notif
'
'NotifyIcon1
'
Me.NotifyIcon1.Icon = CType(resources.GetObject(
Me.NotifyIcon1.Text = "docusyst Processor"
Me.NotifyIcon1.Visible = True
'
'dsTokProcessorInfo
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 173)
Me.Icon = CType(resources.GetObject(
Me.Name = "dsTokProcessorInfo"
Me.Text = "dsTokProcessorInfo"
Me.TopMost = True
Me.WindowState = System.Windows.Forms.FormW
End Sub
#End Region
Private Sub dsTokProcessorInfo_Load(By
AddHandler SystemEvents.SessionEnding
AddHandler SystemEvents.SessionEnded,
Dim endsessionHandler As SessionEndedEventHandler
AddHandler SystemEvents.SessionEnded,
Me.Hide()
End Sub
Public Shared Sub OnShuttingdown(ByVal sender As Object, ByVal e As SessionEndingEventArgs)
msgbox("Shutting Down")
End Sub
Public Shared Sub OnShutdown(ByVal sender As Object, ByVal e As SessionEndedEventArgs)
msgbox("Shut Down")
End Sub
Let me change my answer slightly. I thought I'd used that but I'd forgotten it was awkward to work with because the event isn't guaranteed to be before the closing event and it comes in on a different thread. The easier way is something like this:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Messa ge)
Const WM_QUERYENDSESSION As Integer = &H11
If m.Msg = WM_QUERYENDSESSION Then
Me.m_bSystemShutdownInProg ress = True
End If
MyBase.WndProc(m)
End Sub
and then in your form's closing event you can test m_bSystemShutdownInProgres s to see if you should really shut down. Which now that I look at hairyminga's link, I see it is explained there also.
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Messa
Const WM_QUERYENDSESSION As Integer = &H11
If m.Msg = WM_QUERYENDSESSION Then
Me.m_bSystemShutdownInProg
End If
MyBase.WndProc(m)
End Sub
and then in your form's closing event you can test m_bSystemShutdownInProgres