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
won't be closed. I haven't tried it but I suspect that subscribing to the
"Wil Peck" wrote:
> 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?
>
> "jmagaram" <jmagaram@discussions.microsoft.com> wrote in message
> news:2FCAA6C9-DB6E-4805-ABC5-120990DE035A@microsoft.com...
> > 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.
> >
> > "Wil Peck" wrote:
> >
> >> 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
> >>
> >> "jmagaram" <jmagaram@discussions.microsoft.com> wrote in message
> >> news:37ED51D4-0434-4C06-8DB1-FABF6B0BCC3C@microsoft.com...
> >> > 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?