첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ TaskExtension.cs

using System;
using System.Threading.Tasks;

namespace TestProject
{
    /// <summary>
    /// 태스크 확장
    /// </summary>
    public static class TaskExtension
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 대기하기 - Await(task, completedAction, errorAction)

        /// <summary>
        /// 대기하기
        /// </summary>
        /// <param name="task">태스크</param>
        /// <param name="completedAction">완료시 액션</param>
        /// <param name="errorAction">에러 액션</param>
        public async static void Await(this Task task, Action completedAction, Action<Exception> errorAction)
        {
            try
            {
                await task;

                completedAction?.Invoke();
            }
            catch(Exception exception)
            {
                errorAction?.Invoke(exception);
            }
        }

        #endregion
    }
}

 

728x90

 

▶ 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="Task 클래스 : 태스크 완료시/에러시 처리하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <TextBlock Name="textBlock"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            FontSize="32" />
    </Grid>
</Window>

 

300x250

 

▶ MainWindow.xaml.cs

using System;
using System.Threading.Tasks;
using System.Windows;

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

        #region 생성자 - MainWindow()

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

            Loaded += Window_Loaded;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.textBlock.Text = "기본 값";

            ProcessTask().Await(ProcessCompleted, ProcessError);
        }

        #endregion

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

        #region 태스크 처리하기 - ProcessTask()

        /// <summary>
        /// 태스크 처리하기
        /// </summary>
        /// <returns>태스크</returns>
        private async Task ProcessTask()
        {
            await Task.Delay(3000);

            //throw new Exception("에러 발생");
        }

        #endregion
        #region 완료시 처리하기 - ProcessCompleted()

        /// <summary>
        /// 완료시 처리하기
        /// </summary>
        private void ProcessCompleted()
        {
            this.textBlock.Text = "작업 완료";
        }

        #endregion
        #region 에러시 처리하기 - ProcessError(e)

        /// <summary>
        /// 에러시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        private void ProcessError(Exception e)
        {
            this.textBlock.Text = e.Message;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요