csharp [WPF]别スレッドで别别ン示示示示示示
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp [WPF]别スレッドで别别ン示示示示示示相关的知识,希望对你有一定的参考价值。
<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>
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();
}
}
}
<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>
// 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);
}
}
}
以上是关于csharp [WPF]别スレッドで别别ン示示示示示示的主要内容,如果未能解决你的问题,请参考以下文章
csharp 别スレッドからメインスレッドのコントロールにアクセスするラムダ式
csharp 【WPF】セカンダリスレッド上で発生した未处理の例外の対处
java マルチスレッドch01最大でN个のスレッドまでが実行するパターン
sh マルチスレッド风の处理(击)
java マルチスレッドch01 SingleThreadedExecution
csharp [ワークシートの取得] TryGetWorksheetメソッドでは戻りりででーーーううう