[C#][Application][Event]Application의 시작과 종료 이벤트
* 이벤트 핸들러를 사용하여도 되지만, 그렇게 하지 않고 직접 상속받아서 오버라이드 하여 작성한 예제 소스이다.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
namespace InheritTheApp
{
class InheritTheApp : Application
{
[STAThread]
public static void Main() {
InheritTheApp app = new InheritTheApp();
app.Run();
}
/// <summary>
/// Application의 Run이 실행되면 실행되는 녀석으로 초기화에 유용하다.
/// </summary>
/// <param name="e"></param>
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Window win = new Window();
win.Title = "Inherit the App";
win.Show();
}
/// <summary>
/// 사용자가 윈도우즈를 로그오프 할경우 이벤트가 발생한다.
/// </summary>
/// <param name="e"></param>
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
base.OnSessionEnding(e);
MessageBoxResult result = MessageBox.Show("Do you wanna build a snowman?",
MainWindow.Title,
MessageBoxButton.YesNoCancel,
MessageBoxImage.Question,
MessageBoxResult.Yes);
// 윈도우즈의 종료를 막고 싶다면 Cancel 프로퍼티에 true를 준다.
e.Cancel = ( result == MessageBoxResult.Cancel );
}
/// <summary>
/// 어플리케이션이 종료될때 발생.
/// </summary>
/// <param name="e"></param>
protected override void OnExit(ExitEventArgs e)
{
MessageBox.Show("Application이 종료됩니다.");
base.OnExit(e);
}
}
}