■ 멀티 UI 스레드 윈도우 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


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="멀티 UI 스레드 윈도우 사용하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <StackPanel>

        <StackPanel Margin="10" Orientation="Horizontal">

            <TextBlock Text="스레드 ID : "/>

            <TextBlock Text="{Binding ThreadId}" />

        </StackPanel>

        <Button Name="button"

            Width="200"

            Height="30"

            Content="새 윈도우 생성" />

    </StackPanel>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Threading;

using System.Windows;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            Thread thread = Thread.CurrentThread;

 

            this.DataContext = new

            {

                ThreadId = thread.ManagedThreadId

            };

 

            this.button.Click += button_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #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

            (

                () => {

                    MainWindow window = new MainWindow();

 

                    window.Closed += (sender2, e2) => window.Dispatcher.InvokeShutdown();

 

                    window.Show();

                    

                    System.Windows.Threading.Dispatcher.Run();

                }

            );

 

            thread.SetApartmentState(ApartmentState.STA);

 

            thread.Start();

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요