-1

My problem is what event will file after i kill the Main Process? I have no problem when killing the Sub Process. When i kill the Sub frmAdmin Process,

For eg.

Example Image

This event will be fire,

Private Sub frmAdmin_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If e.CloseReason = CloseReason.TaskManagerClosing Then
        'Put you desired Code inside this!
        'updateLogoutStatus(Euserid)
        'updateLogoutStatus(DecryptAdminUser)
        MsgBox("Why are you terminating me from : TaskManager?")
    End If
End Sub

But when i kill the Main Process, Which contains the sub process,

Example Image 2

Nothing happens. No event fire.

I already searched to internet but they only kill the sub process, not Main process.

Any ideas will be a big help.

The reason why i'm doing this because if the user Kill the main process using Task Manager, I want to trigger a Function.

  • 2
    Why `c#` tag?.. – SᴇM Jan 23 '19 at 06:40
  • 1
    What function do you want to invoke? The end-user is trying to kill you. He/she wants the program dead. They don't want you doing anything. – mjwills Jan 23 '19 at 06:43
  • 2
    Possible duplicate of [C# Windows Form Killed By Task Manager...Is There a Way to Run Shutdown Function?](https://stackoverflow.com/questions/3326242/c-sharp-windows-form-killed-by-task-manager-is-there-a-way-to-run-shutdown-fun) – mjwills Jan 23 '19 at 06:44
  • I want my question to reach a `C#` and `VB.NET` to easily answer my question and any language between `C#` or `VB.NET` is acceptable to me. I can convert those language, vice versa. – Richard Baluyut Jan 23 '19 at 06:46
  • 1
    Killing a process via task Manager is like using `End` in VB code. The process is dumped out of memory unceremoniously rather than shut down properly, which means no events raised in the form(s) or the application. That's exactly why you shouldn't use `End` in VB. If it did raise events, how could you kill a frozen app? – jmcilhinney Jan 23 '19 at 06:53
  • @mjwills, the `Function` i want to run is `updateLogoutStatus` in that function, There is a `Update` query, For eg. If the user login, his/her `login status` will be set to `1` or `True`, the reason why I'm trying to run that function is when He/She kill the process using Task Manager, of course His/Her `login status` will not be change. – Richard Baluyut Jan 23 '19 at 06:54
  • @jmcilhinney do you mean after killing process in task manager no event will fire? – Richard Baluyut Jan 23 '19 at 06:58
  • Hmmm... not sure. Let's see what I said, shall we? *"Killing a process via task Manager is like using End in VB […] which means no events raised in the form(s) or the application"*. Hmmm... that's rather ambiguous so I'm still not sure. smh – jmcilhinney Jan 23 '19 at 07:03
  • 4
    In the battle between users vs programs, Microsoft have decided that Users ultimately win. That's why you cannot write unkillable programs. That's why you sometimes get unceremoniously dumped out with no ability to run any more code. If you think about it, you'll realise that it's the right way around. And if you don't, are you going to start looking next for the event that get's raised when the power goes out killing the entire machine? You have plenty of time when your program *starts* to clean up any residual mess left behind from the last time it ran - look to run code there. – Damien_The_Unbeliever Jan 23 '19 at 07:13

1 Answers1

-1

Here is something I use, similar to your requirement.

Execute the update Query on Form Dispose Event

Private Sub frmAdmin_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
    Try
        'Update query and set flags to true / false'
    Catch ex As Exception
       'log exceptions (if any) to a local log file'
    Finally
        GC.Collect() 'Just release whatever memory your application occupied.'
    End Try
End Sub
Moiyd
  • 55
  • 2
  • 7
  • Thanks for the answer, Yes this will work, But my question is what if I kill the Main Process? As you can see my question has a `vhost-clr2.exe(2)`, this is my Main Process, your solution is for sub process. I have also code for that and also working for me. – Richard Baluyut Jan 23 '19 at 10:01
  • Hi Richard, this is actually working fine for me even when I kill my main process. The Query statement is still get executed. – Moiyd Jan 23 '19 at 10:08