첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.01MB

▶ 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="Dispatcher 클래스 : BeginInvoke 메소드 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <CheckBox Name="checkBox"
                Content="테스트" />
            <Button Name="button"
                Margin="0 10 0 0"
                Width="100"
                Height="30"
                Content="실행" />
        </StackPanel>
    </Grid>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

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

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

        #region 생성자 - MainWindow()

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

            this.button.Click += button_Click;
        }

        #endregion

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

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

        /// <summary>
        /// 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void button_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(ExecuteThread));

            thread.Start();
        }

        #endregion
        #region 디스패처 연산 완료시 처리하기 - dispatcherOperation_Completed(sender, e)

        /// <summary>
        /// 디스패처 연산 완료시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void dispatcherOperation_Completed(object sender, EventArgs e)
        {
            MessageBox.Show(this, "비동기 호출을 완료했습니다.", "INFORMATION", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        #endregion

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

        #region 스레드 실행하기 - ExecuteThread()

        /// <summary>
        /// 스레드 실행하기
        /// </summary>
        private void ExecuteThread()
        {
            DispatcherOperation dispatcherOperation = this.checkBox.Dispatcher.BeginInvoke
            (
                new Action(() => { this.checkBox.IsChecked = !this.checkBox.IsChecked; }),
                DispatcherPriority.Normal
            );

            dispatcherOperation.Completed += dispatcherOperation_Completed;
        }

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

댓글을 달아 주세요