all groups > dotnet windows forms > november 2007 >
You're in the

dotnet windows forms

group:

how to stop my app from closing


how to stop my app from closing jmagaram
11/26/2007 3:15:03 PM
dotnet windows forms:
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,
Re: how to stop my app from closing Jeff Gaines
11/26/2007 3:58:56 PM
On 26/11/2007 in message
[quoted text, click to view]

I think you need to delve into the API.
Look at WM_QueryEndSession and WM_EndSession in the (unfiltered) help.

--
Re: how to stop my app from closing Wil Peck
11/26/2007 4:09:43 PM
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

[quoted text, click to view]
Re: how to stop my app from closing jmagaram
11/26/2007 4:50:03 PM
My app has no need to save changes. I see the SessionEnding event but don't
know how I'd use it. Based on more web searches, I think the answer might be
SetProcessShutdownParameters API. See:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1918073&SiteID=1

If there is a simpler solution using the managed SessionEnding event please
let me know.

[quoted text, click to view]
Re: how to stop my app from closing Wil Peck
11/26/2007 5:36:11 PM
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?

[quoted text, click to view]
Re: how to stop my app from closing jmagaram
11/27/2007 9:10:01 AM
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.

[quoted text, click to view]
AddThis Social Bookmark Button