How do I determine when another process (an application or the OS) is killing my app?

M

Matt Young

Problem: I need to save data before my application is closed by the OS or
another application (killing my app).

The two main scenarios I'm thinking about are,
1. The user navigates to Settings -> System tab -> Memory -> Running
Programs and stops my app (PPC 2003)
2. The user navigates to a utility program like the Dell Switcher Bar (free
with Dell Axims) and stops my app

Currently, I determine when the OS is killing my app (Memory -> Running
Programs and stop) by tracking when all my forms are 'deactivated'. When the
Form.Closing event fires for any form it calls a method which checks if all
the forms are 'deactivated' and if so, it closes my app. This had worked
well, until .

When the user navigates to an application like the Dell Switcher Bar (which
is accessible as a drop down menu like the start menu), the deactivated
event on my form doesn't fire. So when the user asks the Switcher Bar to
kill my app, my application thinks the form 'ok' button was clicked and
closes that form, leaving the application running. Then a warning screen
appears indicating that my application is no longer responding and . the
user has the option to 'kill it' and the application data I need to save is
lost.

Is there any way to determine if another process is killing my app? Any
ideas or suggestions?

Related: I tried using the
OpenNET.Windows.Forms.ApplicationEx.ApplicationExit (v1.1) event but my
attached method never fired.

Thanks!
 
S

Sergey Bogdanov

Chris, WM_CLOSE is already catched by Form.WndProc that invokes Closing,
Closed events. I'm not sure that adding an IMessageFilter will help.

It make sense to look WM_CANCELMODE, if it was one of the last messages
then the user press "close button":

public class MessageFilter : IMessageFilter
{
private void Run(object o)
{
Thread.Sleep(100);
Form1.CancelMode = false;
}

public bool PreFilterMessage(ref Message m)
{
if ( m.Msg == 0x001F)
{
Form1.CancelMode = true;
ThreadPool.QueueUserWorkItem(new WaitCallback(Run));
}

return false;
}
}

And your form code:

internal static bool CancelMode = false;
....
private void Form1_Closing(object sender, CancelEventArgs e)
{
if (!CancelMode)
{
MessageBox.Show("Program Termination");
}
}


Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top