Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm sorry for the trivial question. How I can deallocate resources when the process gets killed by, for example, the Task Manager? Is there a way to call a function before the process gets closed? Thanks in advance Thanks to all for your answers. I think I will implement a watchdog process as suggested.
Posted
Updated 27-Mar-13 1:03am
v2
Comments
Anil Honey 206 27-Mar-13 6:55am    
What you want i have not understand
navin ks 27-Mar-13 6:57am    
when a user close my application through task manager i want to show "save changes before closing"
Anil Honey 206 27-Mar-13 7:21am    
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.TaskManagerClosing)
{
// Task Manager is closing the application
}
else if (e.CloseReason == CloseReason.WindowsShutDown)
{
// application is closed because Windows is shutting down
}
else if (e.CloseReason == CloseReason.UserClosing)
{
// user closes the form
}
}
This Code Works just check it.
navin ks 27-Mar-13 7:28am    
i got it from programfox.. copycat kumari
Anil Honey 206 27-Mar-13 7:40am    
ha ha for Understanding the Concept for programfox i have voted 5 and even i checked in sample application and i updated You and eve iam not a copy cat navin..

Hi,

If you use Windows Forms, then you can use the Form.FormClosing[^] event:
C#
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.TaskManagerClosing)
    {
        // Task Manager is closing the application
    }
    else if (e.CloseReason == CloseReason.WindowsShutDown)
    {
        // application is closed because Windows is shutting down
    }
    else if (e.CloseReason == CloseReason.UserClosing)
    {
        // user closes the form
    }
    else
    {
       // other reason
    }
}

Here you'll find all enum fields of the CloseReason enum: http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason.aspx[^]
If you use WPF, use the Window.Closing[^] event.
But if Task Manager closes the application, then the function will be called only if the user clicks on the "End Task" button. Clicking on the "End Task" button is similar to the Process.Close[^] method. If the user clicks on the "End Process" button, then the process will be stopped immediately, then you can't deallocate resources. Clicking on the "End Process" button is similar to the Process.Kill method[^]

Hope this helps.
 
Share this answer
 
v3
Comments
navin ks 27-Mar-13 7:10am    
+3 thanks
Have you tried capturing the WM_CLOSE event which is raised by Windows?

There is an external article on handling this here
 
Share this answer
 
v2
Comments
navin ks 27-Mar-13 7:07am    
yes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900