how to stop my app from closing

  • Thread starter Thread starter jmagaram
  • Start date Start date
J

jmagaram

i have written a c# .net app that runs in the notification area of the
taskbar. it should stay alive as long as the windows session is alive (like
the volume control or clock in the taskbar). the problem is that my app
closes on Log Off even if the Log Off is aborted. for example, suppose the
user is running Notepad, has a document open with unsaved changes, and then
initiates a Log Off. Notepad pops up a "Save Changes?" dialog; if the user
cancels it Notepad stays running and the Log Off is aborted. But my app is
closed. how can i ensure my app is running as long as the session is alive,
but gracefully shut it down when all other apps have committed to closing?
 
i have written a c# .net app that runs in the notification area of the
taskbar. it should stay alive as long as the windows session is alive (like
the volume control or clock in the taskbar). the problem is that my app
closes on Log Off even if the Log Off is aborted. for example, suppose the
user is running Notepad, has a document open with unsaved changes, and then
initiates a Log Off. Notepad pops up a "Save Changes?" dialog; if the user
cancels it Notepad stays running and the Log Off is aborted. But my app is
closed. how can i ensure my app is running as long as the session is alive,
but gracefully shut it down when all other apps have committed to closing?

I think you need to delve into the API.
Look at WM_QueryEndSession and WM_EndSession in the (unfiltered) help.
 
Check the type Microsoft.Win32.SystemEvents for the SessionEnding event.
Just add a handler to this event in your Program class. The
SessionEndingEventArgs has a Cancel property which allows you to cancel the
shutdown, or alternatively you can provide the user with a dialog allowing
them to save their changes before the application closes.

HTH, Wil
 
When your application initializes - lets say in the Main method - add a
handler to the SessionEnding event.

SystemEvents.SessionEnding += new
SessionEndingEventHandler(SystemEvents_SessionEnding);

In the event handler you can add an Application.Exit which will gracefully
close your application.

static void SystemEvents_SessionEnding(object sender,
SessionEndingEventArgs e)
{
Application.Exit();
}

Depending on what your application is doing at the current time - like
running something on a background thread, you could check your
BackgroundWorker instance IsBusy property to determine if the worker is
currently processing. Using this property you could cancel the log off by
setting the e.Cancel = true; as follows.

static void SystemEvents_SessionEnding(object sender,
SessionEndingEventArgs e)
{
if (myBackgroundWorker.IsBusy)
{
e.Cancel = true;
}
Application.Exit();
}

This should cause the log off to cancel so your program can gracefully
shutdown. I'm not sure what you're asking for in regards to ensuring your
application does not shutdown. Can you elaborate?
 
SetProcessShutdownParameters works - I tried it. I set the shutdown priority
so that my app gets shutdown after Notepad and other apps. So if the user is
running Notepad, has unsaved changes, Logs off, and then clicks Cancel in the
Notepad "save changes?" dialog - thereby cancelling the Log Off - my app
won't be closed. I haven't tried it but I suspect that subscribing to the
SessionEnded event won't help because my app will probably be closed before
the event is raised.
 
Back
Top