我试图在特定的蓝牙消息到达时在我的应用程序中显示一个菜单元素。通过定时器方法收集和解释消息,并且如果正确的消息到达,则该元素应该呈现为可见。我不断收到一个异常,告诉我该对象是由另一个线程拥有的,无法访问。从不同的线程访问GUI线程抛出异常
// Shows a TangibleMenu element
private void Show(TangibleMenu TangibleMenuElement)
{
if (TangibleMenuElement.Shape.CheckAccess())
{
Debug.WriteLine("normal show");
TangibleMenuElement.Shape.Opacity = 1;
TangibleMenuElement.Shape.Visibility = System.Windows.Visibility.Visible;
this.ParentContainer.Activate(TangibleMenuElement.Shape);
}
else
{
Dispatcher.CurrentDispatcher.Invoke(new Action(() =>
{
Debug.WriteLine("dispatcher show");
TangibleMenuElement.Shape.Opacity = 1; // EXCEPTION HERE
TangibleMenuElement.Shape.Visibility = System.Windows.Visibility.Visible;
this.ParentContainer.Activate(TangibleMenuElement.Shape);
}));
}
}
我认为这个确切的问题可以通过使用调度器来解决,但在这种情况下,它似乎并没有工作。 TangibleMenuElement.Shape
是Microsoft Surface SDK中的ScatterViewItem。有没有人有什么建议?
是'Dispatcher'线程创建'TangibleMenuElement'的那个线程。 –
不,我想改变已经和另一个线程创建的对象的一些属性。我怎么做? – xmashallax