C# WPF MainWindow 引用的行为与相同的备用引用不同
C# WPF MainWindow Reference behaves differently to identical alternate reference
我创建了一个最小的 WPF 示例,展示了我在更复杂的容量中发现的一个令人困惑的问题。本质上,从外部线程使用 Application.Current.MainWindow
获取对应用程序 Main Window 的引用可靠地抛出 InvalidOperationException 以进行跨线程对象访问。 简单阅读参考资料。然而!如果给定的 MainWindow 在字段中存储了对自身的引用,则可以毫无问题地读取该引用。它甚至可以被取消引用,例如访问其属性之一。其他 window 字段也可以从其他线程读取和操作。为什么获得相同的引用(用引用等于确认)是跨线程攻击还是不是跨线程攻击取决于它的阅读方式? Application.Current
是线程安全的,所以无可厚非。 MainWindow
属性 没有说明对其 getter 进行关联检查的文档。为什么会这样?
XAML 实验:
<Window x:Class="WPF_TEST.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_TEST"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="Linen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Row="1" Grid.Column="1" Content="Test Handlers" Click="IReliablyExcept">
</Button>
</Grid>
</Window>
CODE_BEHIND 用于实验
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF_TEST
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly MainWindow mainWindowField;
private bool someFlag = true;
public MainWindow()
{
mainWindowField = this;
InitializeComponent();
}
private async void IReliablyExcept(object sender, RoutedEventArgs e)
{
await Task.Run(() =>
{
var winRef = Application.Current.MainWindow;
});
}
private async void ButIDont(object sender, RoutedEventArgs e)
{
await Task.Run(() =>
{
var winRef = mainWindowField;
});
}
private async void IDontEither(object sender, RoutedEventArgs e)
{
await Task.Run(() =>
{
if (someFlag)
{
mainWindowField.Dispatcher.Invoke(() => { someFlag = false; });
}
});
}
}
}
可以重新连接 Click 事件处理程序以尝试所有三种变体。感谢您的任何见解。
就是这样,因为 Application.Current.MainWindow
属性 检查调用线程是否有访问权限。
这是来自反编译器的 属性 的代码,this.VerifyAccess();
将可靠地抛出异常。
public Window MainWindow
{
get
{
this.VerifyAccess();
return this._mainWindow;
}
set
{
this.VerifyAccess();
if (this._mainWindow is RootBrowserWindow || this.BrowserCallbackServices != null && this._mainWindow == null && !(value is RootBrowserWindow))
throw new InvalidOperationException(SR.Get("CannotChangeMainWindowInBrowser"));
if (value == this._mainWindow)
return;
this._mainWindow = value;
}
}
我创建了一个最小的 WPF 示例,展示了我在更复杂的容量中发现的一个令人困惑的问题。本质上,从外部线程使用 Application.Current.MainWindow
获取对应用程序 Main Window 的引用可靠地抛出 InvalidOperationException 以进行跨线程对象访问。 简单阅读参考资料。然而!如果给定的 MainWindow 在字段中存储了对自身的引用,则可以毫无问题地读取该引用。它甚至可以被取消引用,例如访问其属性之一。其他 window 字段也可以从其他线程读取和操作。为什么获得相同的引用(用引用等于确认)是跨线程攻击还是不是跨线程攻击取决于它的阅读方式? Application.Current
是线程安全的,所以无可厚非。 MainWindow
属性 没有说明对其 getter 进行关联检查的文档。为什么会这样?
XAML 实验:
<Window x:Class="WPF_TEST.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_TEST"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="Linen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Row="1" Grid.Column="1" Content="Test Handlers" Click="IReliablyExcept">
</Button>
</Grid>
</Window>
CODE_BEHIND 用于实验
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF_TEST
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly MainWindow mainWindowField;
private bool someFlag = true;
public MainWindow()
{
mainWindowField = this;
InitializeComponent();
}
private async void IReliablyExcept(object sender, RoutedEventArgs e)
{
await Task.Run(() =>
{
var winRef = Application.Current.MainWindow;
});
}
private async void ButIDont(object sender, RoutedEventArgs e)
{
await Task.Run(() =>
{
var winRef = mainWindowField;
});
}
private async void IDontEither(object sender, RoutedEventArgs e)
{
await Task.Run(() =>
{
if (someFlag)
{
mainWindowField.Dispatcher.Invoke(() => { someFlag = false; });
}
});
}
}
}
可以重新连接 Click 事件处理程序以尝试所有三种变体。感谢您的任何见解。
就是这样,因为 Application.Current.MainWindow
属性 检查调用线程是否有访问权限。
这是来自反编译器的 属性 的代码,this.VerifyAccess();
将可靠地抛出异常。
public Window MainWindow
{
get
{
this.VerifyAccess();
return this._mainWindow;
}
set
{
this.VerifyAccess();
if (this._mainWindow is RootBrowserWindow || this.BrowserCallbackServices != null && this._mainWindow == null && !(value is RootBrowserWindow))
throw new InvalidOperationException(SR.Get("CannotChangeMainWindowInBrowser"));
if (value == this._mainWindow)
return;
this._mainWindow = value;
}
}