Task manager - End Process Detection  
Author Message
CodeButcher





PostPosted: Visual Basic General, Task manager - End Process Detection Top

I have a program that logs whenever it is shutdown. I can capture that process if the user simply closes the application or uses the "end task" option in the task manager.

if they use the task manager "End Process" then, naturally, the program cannot detect that. Therefore, I used a bit of creative logic. When the program opens, it sets a flag in a file. When it closes it sets the flag to something else. Therefore, when the program opens up the next time, if the flag isn't set to the proper value, the program concludes it was forced closed such as with the End Process option of task manager.

The problem is if the user has their pc freeze up and has to do a cold boot, the software shows this as an abnormal shutdown. Due to the nature of the program, such a false recording can have severe negative effects.

What can I do so my program can detect an "End Process " Using a secondary application might be considered if you can explain how the two can be used in conjunction. OR, as a more general question, what can I do to differenciate a freeze-up from a program End Process



Visual Basic12  
 
 
DMan1





PostPosted: Visual Basic General, Task manager - End Process Detection Top

You can monitor an external process and keep an instance of the process alive with the following code...It does work when shut down with "end process" also



Dim MyMonitoredProcess As Process = Nothing
Dim ProcessName As String = "Notepad"
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim p() As Process = Process.GetProcessesByName(ProcessName)
If p.Length > 0 Then
MyMonitoredProcess = p(0)

MyMonitoredProcess.EnableRaisingEvents = True
AddHandler MyMonitoredProcess.Exited, AddressOf TestProcessEndedEvent
End If
End Sub
Private Sub TestProcessEndedEvent(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("Ending")
MyMonitoredProcess = Process.Start(ProcessName)
MyMonitoredProcess.EnableRaisingEvents = True
AddHandler MyMonitoredProcess.Exited, AddressOf TestProcessEndedEvent
End Sub

 

 

edit: Clarification...this does not stop the user from shutting down the app using "End Process" it detects it and then starts another instance...



 
 
CodeButcher





PostPosted: Visual Basic General, Task manager - End Process Detection Top

This is very helpful.  One question...when the computer shuts down, if program A was monitered and Program B was the monitoring program...when a user turns off the computer, if program A was automatically turned off, program B would start it again and then program B would turn off and where would that leave process A   Would it hang the application or again shut it down normally a second time

Edit: here's what i'm thinking.  I don't want the user to turn off the application or the monitoring application...so would they monitor each other...then I'd have a looping problem when the user tried to turn off the computer.