첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainApplication.xaml

<Application x:Class="TestProject.MainApplication"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml" 
    ShutdownMode="OnExplicitShutdown"
    Exit="Application_Exit">
</Application>

 

▶ MainApplication.xaml.cs

using System;
using System.Collections;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 메인 애플리케이션
    /// </summary>
    public partial class MainApplication : Application
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 애플리케이션 종료시 처리하기 - Application_Exit(sender, e)

        /// <summary>
        /// 애플리케이션 종료시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Application_Exit(object sender, ExitEventArgs e)
        {
            try
            {
                if(e.ApplicationExitCode == (int)ApplicationExitCode.Success)
                {
                    WriteApplicationLogEntry("Success", e.ApplicationExitCode);
                }
                else
                {
                    WriteApplicationLogEntry("Failure", e.ApplicationExitCode);
                }
            }
            catch
            {
                e.ApplicationExitCode = (int)ApplicationExitCode.CanNotWriteToApplicationLog;
            }

            try
            {
                WriteApplicationState();
            }
            catch
            {
                e.ApplicationExitCode = (int)ApplicationExitCode.CanNotWriteApplicationState;
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 애플리케이션 로그 엔트리 쓰기 - WriteApplicationLogEntry(message, exitCode)

        /// <summary>
        /// 애플리케이션 로그 엔트리 쓰기
        /// </summary>
        /// <param name="message">메시지</param>
        /// <param name="exitCode">종료 코드</param>
        private void WriteApplicationLogEntry(string message, int exitCode)
        {
            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly();

            using(IsolatedStorageFileStream stream = new IsolatedStorageFileStream("log.txt", FileMode.Append, FileAccess.Write, file))
            {
                using(StreamWriter writer = new StreamWriter(stream))
                {
                    string entry = string.Format
                    (
                    
                        "[{0}] : {1} - {2}",
                        DateTime.Now.ToString("HH:mm:dd"),
                        exitCode,
                        message
                    );

                    writer.WriteLine(entry);
                }
            }
        }

        #endregion
        #region 애플리케이션 상태 쓰기 - WriteApplicationState()

        /// <summary>
        /// 애플리케이션 상태 쓰기
        /// </summary>
        private void WriteApplicationState()
        {
            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly();

            using(Stream stream = new IsolatedStorageFileStream("state.txt", FileMode.Create, file))
            {
                using(StreamWriter writer = new StreamWriter(stream))
                {
                    foreach(DictionaryEntry entry in Properties)
                    {
                        writer.WriteLine(entry.Value);
                    }
                }
            }
        }

        #endregion
    }
}

 

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="600"
    Title="Application 클래스 : Shutdown 메소드를 사용해 명시적 애플리케이션 종료하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid Margin="10">
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <StackPanel
                Margin="10"
                Orientation="Horizontal">
                <Label
                    Margin="10"
                    FontWeight="Bold"
                    Content="종료 코드" />
                <RadioButton Name="successRadioButton"
                    VerticalAlignment="Center"
                    Margin="10"
                    IsChecked="True"
                    Content="성공" />
                <RadioButton Name="failureRadioButton"
                    VerticalAlignment="Center"
                    Margin="10"
                    Content="실패" />
            </StackPanel>
            <Button Name="exitButton"
                Margin="10"
                Width="100"
                Height="30"
                Content="종료하기" />
        </StackPanel>
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            this.exitButton.Click += exitButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 종료하기 버튼 클릭시 처리하기 - exitButton_Click(sender, e)

        /// <summary>
        /// 종료하기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void exitButton_Click(object sender, RoutedEventArgs e)
        {
            int exitCode = (this.successRadioButton.IsChecked == true ? 0 : 1);

            Application.Current.Shutdown(exitCode);
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요