";调用线程无法访问此对象,因为另一个线程拥有它;
本文关键字:线程 因为 另一个 拥有 对象 访问 调用 quot | 更新日期: 2023-09-27 18:28:20
我有一个WPF应用程序,它使用多线程来执行通知。但我遇到了"调用线程无法访问此对象,因为其他线程拥有它。"异常的问题。
当我的主窗口加载时,我为通知窗口创建一个新线程
private void RibbonWindow_Loaded(object sender, RoutedEventArgs rae)
{
Thread newWindowThread = new Thread(new ThreadStart(() =>
{
var reminders = new List<Reminder>();
// Go to the database to get active reminders...
var notificationsViewModel = new NotificationsViewModel(reminders);
var notificationsView = new NotificationsView();
notificationsView.DataContext = notificationsViewModel;
var checkAccess = notificationsView.Dispatcher.CheckAccess();
if (checkAccess)
{
notificationsView.Show();
}
System.Windows.Threading.Dispatcher.Run();
}));
newWindowThread.IsBackground = true;
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.Start();
}
视图的构造函数为默认值:
public partial class NotificationsView : Window
{
public NotificationsView()
{
InitializeComponent();
}
}
视图模型和绑定的模型的构造函数
public NotificationsViewModel(List<ReminderCommon> reminders)
{
this.Reminders = reminders;
this.ReminderModel = reminders.First();
}
public ReminderCommon ReminderModel
{
get
{
return this.m_ReminderModel;
}
set
{
this.m_ReminderModel = value;
base.OnPropertyChanged("ReminderModel");
}
}
最后,我的观点
<Border BorderThickness="1" Background="Beige" BorderBrush="DarkKhaki" CornerRadius="5">
<StackPanel Margin="5">
<TextBlock Text="{Binding ReminderModel.Message}" TextWrapping="NoWrap" Margin="5" FontWeight="Bold"></TextBlock>
<TextBlock Text="{Binding ReminderModel.DateFormatted}" TextWrapping="NoWrap" Margin="5"></TextBlock>
<TextBlock Text="{Binding ReminderModel.Patient}" TextWrapping="NoWrap" Margin="5"></TextBlock>
<TextBlock Text="{Binding ReminderModel.PatientPhoneNumbers}" TextWrapping="Wrap" Margin="5"></TextBlock>
<TextBlock Text="{Binding ReminderModel.Doctor}" TextWrapping="NoWrap" Margin="5"></TextBlock>
<CheckBox IsChecked="{Binding ReminderModel.IsCompleted}" Margin="5" />
</StackPanel>
</Border>
</Border>
我在上收到异常
notificationsView.Show();
异常详细信息
at System.Windows.Threading.Dispatcher.VerifyAccess()
at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
at System.Windows.Media.SolidColorBrush.get_Color()
at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
at System.Windows.UIElement.Arrange(Rect finalRect)
at System.Windows.Controls.Primitives.BulletDecorator.ArrangeOverride(Size arrangeSize)
at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
at System.Windows.UIElement.Arrange(Rect finalRect)
at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)
at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
at System.Windows.UIElement.Arrange(Rect finalRect)
at System.Windows.Controls.StackPanel.ArrangeOverride(Size arrangeSize)
at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
at System.Windows.UIElement.Arrange(Rect finalRect)
at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
at System.Windows.UIElement.Arrange(Rect finalRect)
at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
at System.Windows.UIElement.Arrange(Rect finalRect)
at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)
at System.Windows.Controls.ContentPresenter.ArrangeOverride(Size arrangeSize)
at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
at System.Windows.UIElement.Arrange(Rect finalRect)
at System.Windows.Documents.AdornerDecorator.ArrangeOverride(Size finalSize)
at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
at System.Windows.UIElement.Arrange(Rect finalRect)
at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
at System.Windows.UIElement.Arrange(Rect finalRect)
at System.Windows.Window.ArrangeOverride(Size arrangeBounds)
at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
at System.Windows.UIElement.Arrange(Rect finalRect)
at System.Windows.Interop.HwndSource.SetLayoutSize()
at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at System.Windows.Window.SetRootVisual()
at System.Windows.Window.SetRootVisualAndUpdateSTC()
at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
at System.Windows.Window.CreateSourceWindowDuringShow()
at System.Windows.Window.SafeCreateWindowDuringShow()
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at Dentist.MainWindow.<RibbonWindow_Loaded>b__0() in D:'development'Dental Soft'Lotus'AMS'Dentist'MainWindow.xaml.cs:line 210
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
TargetSite: {Void VerifyAccess()}
我还提到了两件奇怪的事情:这段代码在Windows8中完全可以工作,但在Windows7和WindowsXP中不起作用,当我从视图中删除<CheckBox IsChecked="{Binding ReminderModel.IsCompleted}" Margin="5" />
时,代码就起作用了。如果我使用<Button />
(只是控制,没有任何绑定!)
您不需要另一个线程来并行运行另一个窗口。只在后台线程上执行与数据相关的操作,不在那里创建GUI元素。