Questions About Form Closing Event In winform
-
I want to write a winform program to implement following behaviour when the program exits,I will write some log infomation in a log file so I write some code in Form_Closing Function,It works fine when I click X on the window.But when I shutdown the computer before I close the program,It seems Form_Closing Event is not fired,so the log information is not saved into file.I checked many articles on MSDN And Google,It all says,this event will be triggered when shutting down the computer,I also tried to handle the WM_QUERYENDSESSION message,it did not work either. Can Anyone give some suggestions and help? thanks
-
I want to write a winform program to implement following behaviour when the program exits,I will write some log infomation in a log file so I write some code in Form_Closing Function,It works fine when I click X on the window.But when I shutdown the computer before I close the program,It seems Form_Closing Event is not fired,so the log information is not saved into file.I checked many articles on MSDN And Google,It all says,this event will be triggered when shutting down the computer,I also tried to handle the WM_QUERYENDSESSION message,it did not work either. Can Anyone give some suggestions and help? thanks
Hi, AFAIK you should: - have a "LogClosing" method that logs what needs to be logged; - have that called by the SystemEvents.SessionEnding handler; - have it also called by the Form.FormClosing handler; - implement some protection so it only executes once (as FormClosing may or may not fire on session end) :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.