I want to call a c# function when my application is closed using task manager or abruptly.
I want to perform some operations there.
Please Help me.
Thanks in advance.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Vivek Arya20 reputationPosted Apr 29, 2013, 11:03 PM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 30, 2013, 1:19 AM EDT
Unfortunately, the Window.Closing or Closed event in WPF doesn't have this and you therefore have to resort to examining the underlying Win32 mesage to get this information.
I found this thread which explains how to do it:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/549a4bbb-e77b-4c5a-b724-07996774c60a
Vivek Arya20 reputationPosted Apr 30, 2013, 1:35 AM EDT
It does not show any type of message that application is going to close, when the application is closed using Task Manager.
VulpesRank #55 · 29.2K reputationPosted Apr 30, 2013, 3:16 AM EDT
So to determine which applies you have to set CloseReason to EndTask by default and then when the user performs some action such as clicking a button to close the form, you have to manually set CloseReason to User.
If CloseReason is still set to EndTask when the form closes then it must have been closed via Task Manager.
It's not very satisfactory but it seems to be the only way to do it.
VulpesRank #55 · 29.2K reputationPosted Apr 30, 2013, 3:10 PM EDT
So there's no problem trapping these actions and the only ones where you need to set CloseReason.User manually are those where the form is closed programatically.