Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there OnSutdown event for a Console app type?

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
566
US
Colleagues,
The subject line says it.
There is OnShutdown event for the Win Forms type (used it myself for such projects), but I couldn't find how to catch this event in Console type app.
In case you wonder: imagine you need to run a Console app for some period of time, and something (or someone) shuts it off "prematurely"... and you need to know (say, a line in the program's very own LOG file) when the Program's run completed...

AHWBGA!

Regards,

Ilya
 
One method is to add a handler for AppDomain.ProcessExit event
 
StrongM said:
add a handler for AppDomain.ProcessExit event
Ran search for AppDomain.ProcessExit event on MS Help - no comprehensive (let alone useful) information found.
Ran search on I-Net - useful information not found either.
So I'm "in helicopter" (if you know what I mean)... :-(

Could you, please, elaborate?

Regards,

Ilya
 
Yes, StrongM, I've seen this.
The problem I have - it's not of a help for me. (Like I've said, "You are in a helicopter".)
Would you be so kind to provide a sample code snippet?


Regards,

Ilya
 
Code:
[COLOR=blue]Module Program
    Sub Main(args As String())
        ' Attach the event handler method
        AddHandler AppDomain.CurrentDomain.ProcessExit, AddressOf MyApp_ProcessExit
        Console.WriteLine("Hello World!")
    End Sub

    Private Sub MyApp_ProcessExit(sender As Object, e As EventArgs)
        Console.WriteLine("App Is Exiting...")
    End Sub
End Module[/color]
 
OK, I added

Code:
AddHandler AppDomain.CurrentDomain.ProcessExit, AddressOf FWC_Exit

to the Main(), then added

Code:
'====================================================================================================================================
Private Sub FWC_Exit(sender As Object, e As EventArgs)
'====================================================================================================================================
Dim lsLogStr As String = Replicate("=", 80) + vbCrLf + Format(Now, "yyyy-MM-dd HH:mm:ss") + ": File Watcher quit"

lsLogStr += vbCrLf + Replicate("=", 80) + vbCrLf
My.Computer.FileSystem.WriteAllText(gsLogFile, lsLogStr, True)

End Sub
'====================================================================================================================================

ran (F5) the Program, then hit the "X" button on the console window. Window closed, Debug run stopped, but nothing was written into the LOG file...
Just in case: I have
Code:
lsLogStr = Replicate("=", 80) + vbCrLf + Format(Now, "yyyy-MM-dd HH:mm:ss") + ": File Watcher started on " + _
				gcMachineName + vbCrLf + Replicate("=", 80) + vbCrLf
My.Computer.FileSystem.WriteAllText(gsLogFile, lsLogStr, True)

at the top of the Main(), and this part works.

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top