[英]“The calling thread cannot access this object because a different thread owns it”
I have a WPF application that uses multi-threading to perform notifications. 我有一个使用多线程执行通知的WPF应用程序。 But I've faced the problem with "The calling thread cannot access this object because a different thread owns it." 但是我遇到了“调用线程无法访问该对象,因为另一个线程拥有它的问题”。 exception. 例外。
When my main window is loaded I create a new thread for notification window 加载主窗口后,我会为通知窗口创建一个新线程
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();
}
Constructor for view is default: 视图的构造函数是默认的:
public partial class NotificationsView : Window
{
public NotificationsView()
{
InitializeComponent();
}
}
Constructor for view model and model to bind 视图模型的构造方法和要绑定的模型
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");
}
}
Finally, my view 最后,我的看法
<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>
I receive exception on 我收到例外
notificationsView.Show();
Exception details 例外详情
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()}
Also I mentioned two strange things: this code perfectly works in Windows 8, but doesn't work in Windows 7 and Windows XP and when I delete <CheckBox IsChecked="{Binding ReminderModel.IsCompleted}" Margin="5" />
from view code works. 我还提到了两点奇怪的事情:该代码在Windows 8中完美地工作,但是在Windows 7和Windows XP中不工作,并且当我从中删除<CheckBox IsChecked="{Binding ReminderModel.IsCompleted}" Margin="5" />
时查看代码的作品。 The same exceptions occurs if I use <Button />
(just control, without any bindings!) 如果我使用<Button />
则会发生相同的异常(只是控制,没有任何绑定!)
You do not need another thread just to have another window running in parallel. 你并不需要另一个线程正好有并行运行的另一个窗口。 Only do data-related actions on a background thread, do not create GUI elements there. 仅在后台线程上执行与数据相关的操作,而不在后台线程上创建GUI元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.