Created
April 20, 2016 11:36
-
-
Save devlights/763fed146ffe8cb65b78cbfd155da820 to your computer and use it in GitHub Desktop.
[WPF] 別スレッドで個別にイベントスレッドを起動して画面を表示 (SetWindowLong, Dispather.Run, Dispatcher.InvokeShutdown)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ReSharper disable PossibleInvalidOperationException | |
| namespace WpfViewAnotherThread | |
| { | |
| using System; | |
| using System.Diagnostics; | |
| using System.Runtime.InteropServices; | |
| using System.Threading; | |
| using System.Windows; | |
| using System.Windows.Interop; | |
| using System.Windows.Threading; | |
| public partial class MainWindow : Window | |
| { | |
| private const int GWL_HWNDPARENT = -8; | |
| private Thread _viewAThread; | |
| public MainWindow() | |
| { | |
| this.InitializeComponent(); | |
| } | |
| public static IntPtr GetHandler(Window window) | |
| { | |
| var interop = new WindowInteropHelper(window); | |
| return interop.Handle; | |
| } | |
| public static void SetOwnerWindowMultithread(IntPtr windowHandleOwned, IntPtr intPtrOwner) | |
| { | |
| if (windowHandleOwned != IntPtr.Zero && intPtrOwner != IntPtr.Zero) | |
| { | |
| SetWindowLong(windowHandleOwned, GWL_HWNDPARENT, intPtrOwner.ToInt32()); | |
| } | |
| } | |
| [DllImport("user32.dll")] | |
| private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle); | |
| private void BtnA_Click(object sender, RoutedEventArgs e) | |
| { | |
| if (this.ChkAnotherThread.IsChecked.Value) | |
| { | |
| var handler = GetHandler(this); | |
| this._viewAThread = new Thread( | |
| state => | |
| { | |
| var theType = (Type) state; | |
| var win = (Window) Activator.CreateInstance(theType); | |
| win.Closed += (o, args) => | |
| { | |
| Dispatcher.CurrentDispatcher.InvokeShutdown(); | |
| }; | |
| win.Show(); | |
| SetOwnerWindowMultithread(GetHandler(win), handler); | |
| Dispatcher.Run(); | |
| }); | |
| this._viewAThread.IsBackground = true; | |
| this._viewAThread.SetApartmentState(ApartmentState.STA); | |
| this._viewAThread.Start(typeof(ViewA)); | |
| } | |
| else | |
| { | |
| var win = new ViewA(); | |
| win.ShowDialog(); | |
| } | |
| } | |
| private void BtnB_Click(object sender, RoutedEventArgs e) | |
| { | |
| Debug.Assert(this._viewAThread.IsAlive == false); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Window x:Class="WpfViewAnotherThread.MainWindow" | |
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
| Title="MainWindow" Height="350" Width="525"> | |
| <Grid> | |
| <Button x:Name="BtnA" Content="画面A" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="BtnA_Click"/> | |
| <CheckBox x:Name="ChkAnotherThread" IsChecked="True" Content="別スレッドで表示" HorizontalAlignment="Left" Margin="11,39,0,0" VerticalAlignment="Top"/> | |
| <Button x:Name="BtnA_Copy" Content="画面A" HorizontalAlignment="Left" Margin="90,10,0,0" VerticalAlignment="Top" Width="75" Click="BtnB_Click"/> | |
| </Grid> | |
| </Window> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace WpfViewAnotherThread | |
| { | |
| using System.Windows; | |
| public partial class ViewA : Window | |
| { | |
| public ViewA() | |
| { | |
| this.InitializeComponent(); | |
| } | |
| private void BtnShowSub_Click(object sender, RoutedEventArgs e) | |
| { | |
| var win = new ViewSub(); | |
| win.Owner = this; | |
| win.ShowDialog(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Window x:Class="WpfViewAnotherThread.ViewA" | |
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
| Title="ViewA" Height="300" Width="300"> | |
| <Grid> | |
| <TextBlock Text="ViewA" FontSize="40"></TextBlock> | |
| <Button x:Name="BtnShowSub" Content="サブ画面をShowDialog" HorizontalAlignment="Left" Margin="10,77,0,0" VerticalAlignment="Top" Width="Auto" Click="BtnShowSub_Click"/> | |
| </Grid> | |
| </Window> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment