■ 다중 UI 스레드 윈도우 사용하기
------------------------------------------------------------------------------------------------------------------------
▶ 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 } }
|
------------------------------------------------------------------------------------------------------------------------
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Dispatcher 클래스 : UnhandledException 이벤트를 사용해 예외 처리하기 (0) | 2019.06.02 |
---|---|
[C#/WPF] SystemColors 클래스 : 시스템 색상 표시하기 (0) | 2019.06.02 |
[C#/WPF] Dispatcher 클래스 : Invoke 메소드를 사용해 UI 업데이트 하기 (0) | 2019.05.30 |
[C#/WPF] 메모리 손실이 없는 비트맵 소스 구하기 (0) | 2019.05.30 |
[C#/WPF] AppDomain 클래스 : 동일 프로세스에서 멀티 WPF 애플리케이션 실행하기 (0) | 2019.05.30 |
[C#/WPF] 다중 UI 스레드 윈도우 사용하기 (0) | 2019.05.30 |
[C#/WPF] ScrollViewer 클래스 : 마우스를 사용해 스크롤하기 (0) | 2019.05.28 |
[C#/WPF] BitmapImage 클래스 : 웹에서 비트맵 이미지 구하기 (0) | 2019.05.27 |
[C#/WPF] BitmapImage 클래스 : 비트맵 이미지 구하기 (0) | 2019.05.27 |
[C#/WPF] ScrollViewer 클래스 : IsDeferredScrollingEnabled 속성을 사용해 스크롤 성능 높이기 (0) | 2019.05.27 |
[C#/WPF] FrameworkElement 클래스 : RequestBringIntoView 이벤트를 사용해 ScrollViewer 내부 객체 포커스시 자동 스크롤 방지하기 (0) | 2019.05.27 |
댓글을 달아 주세요