SessionEnding

By default, an application shuts down when the Windows session ends, which occurs when a user logs off or shuts down.

You can detect when a session ends by handling the SessionEnding event. If an application needs to prevent the session from ending, the SessionEndingCancelEventArgs argument that is passed to the event handler exposes the Cancel that you set to true (the default value is false).

注意SessionEnding的适用范围:

SessionEnding is not raised by console applications or XAML browser applications (XBAPs) .

SessionEnding is raised only on the thread that creates the Application object.

例子:
在退出windows session的时候弹出对话框让用户选择是否退出。

    public partial class App : Application
    {
       
void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
        {
           
// Ask the user if they want to allow the session to end
            string msg = string.Format("{0}. End session?", e.ReasonSessionEnding);
            MessageBoxResult result
= MessageBox.Show(msg, "Session Ending", MessageBoxButton.YesNo);

           
// End session, if specified
            if (result == MessageBoxResult.No)
            {
                e.Cancel
= true;
            }
        }
    }

 

然后在XAML里配置,让上面的方法与SessionEnding Event相关联。

<Application x:Class="LearnWPF.App"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
    xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml%22
    StartupUri
="Window1.xaml"
    SessionEnding
="App_SessionEnding"> 

</Application>

 

 

DispatcherUnhandledException

Occurs when an unhandled exception occurs anywhere in your application (on the main application thread).
By responding to this event, you can log critical errors.

You can even choose to neutralize the exception and continue running your application by setting the
DispatcherUnhandledExceptionEventArgs.Handled property to true.

指定event handler的方法还是一样,先写好方法:

private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
    MessageBox.Show(
"An unhandled " + e.Exception.GetType().ToString() +
   
" exception was caught and ignored.");
    e.Handled
= true; // 把该异常设置为已处理,以防止程序因此退出
}


再编辑XAML:

<Application x:Class="PreventSessionEnd.App"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri
="Window1.xaml"
    DispatcherUnhandledException
="App_DispatcherUnhandledException"
>
</Application>

 

用override的方式来指定event handler

重载哪个方法?很简单,event叫什么名字,在前面加个On就是了。例如OnStartup,OnSessionEnding。只有DispatcherExceptionUnhandled事件是个例外,没有OnDispatcherExceptionUnhandled方法,只有像上文那样在XAML中明确写出event handler了。

 

public partial class App : Application
{
   
private bool unsavedData = false;
   
public bool UnsavedData
    {
       
get { return unsavedData; }
       
set { unsavedData = value; }
    }
   
protected override void OnStartup(StartupEventArgs e)
    {
       
base.OnStartup(e); // call base first
        UnsavedData = true;
    }
   
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
    {
       
base.OnSessionEnding(e); // call base first
        if (UnsavedData)
        {
            e.Cancel
= true;
            MessageBox.Show(
           
"The application attempted to be closed as a result of " +
            e.ReasonSessionEnding.ToString()
+
           
". This is not allowed, as you have unsaved data.");
        }
    }
}
posted on 2009-11-24 02:18  MainTao  阅读(707)  评论(0编辑  收藏  举报