NotificationObject的调度程序 [英] a dispatcher for NotificationObject

查看:53
本文介绍了NotificationObject的调度程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个NotificationView对象充当视图模型.第一个NotificationObject包含绑定到特定控件视图的属性,第二个NotificationObject包含一些使用Thread对象在另一个线程中执行的代码.我需要从第二个NotificationObject的运行代码中更改第一个NotificationObject的属性.当我尝试执行此操作时,出现异常调用线程无法访问此对象,因为其他线程拥有它.".

I have 2 NotificationObject objects that act as view models. First NotificationObject contains properties that are binded to the view of a specific control, and second NotificationObject has some code that executes in another thread using Thread object. I need to change properties of first NotificationObject from the running code of the second NotificationObject. When I try to do it I get an exception "The calling thread cannot access this object because a different thread owns it.".

我认为我需要使用某种调度程序来访问这些属性,就像在Windows Forms或经典WPF中所做的那样,但是我找不到在Prism MVVM中如何做的事情.那么如何更改第一个NotificationObject的属性?

I think that I need to use some sort of dispatcher to access those properties as I would do in Windows Forms or classic WPF, but I can't find how to do it in Prism MVVM. So how do I change the properties of first NotificationObject?

推荐答案

我猜测您在一个线程上创建了一些东西,并试图从另一个线程更新它,而WPF不允许这样做.只能从创建对象的线程中修改对象.

I am guessing that you have something created on one thread, and are trying to update it from the other thread, and WPF doesn't allow this. Objects can only be modified from the thread they were created on.

通常,所有对象都在主UI线程上创建,并且Dispatcher用于将异步消息传递给UI线程以更新这些对象.仍然可以在后台线程上进行任何繁重的处理,但是要更新对象的属性,您需要使用主UI线程.

Usually all objects are created on the main UI thread, and the Dispatcher is used to pass asynchronous messages to the UI thread to update these objects. Any heavy processing can still be done on the background thread, however to update the property of an object you need to use the main UI thread.

例如,看起来像这样的东西会起作用:

For example, something that looks like this would work:

MyNotificationObject obj = new MyNotificationObject;
obj.Items = MethodThatRunsOnBackgroundThread();

List<SomeObject> MethodThatRunsOnBackgroundThread()
{
    var list = new List<SomeObject>();
    // Do Work
    Return list;
}

这不会:

MyNotificationObject obj = new MyNotificationObject;
MethodThatRunsOnBackgroundThread(obj);

void MethodThatRunsOnBackgroundThread()
{
    var list = new List<SomeObject>();
    // Do Work

    // This won't work since obj was created on UI thread
    obj.Items = List<SomeObject>;
}

这是另一个可行的示例,因为它正在向创建对象的UI线程发送更新对象的消息.

Here is another example that works, because it is sending the message to update the object to the UI thread which created the object.

MyNotificationObject obj = new MyNotificationObject;
MethodThatRunsOnBackgroundThread(obj);

void MethodThatRunsOnBackgroundThread()
{
    var list = new List<SomeObject>();

    // Load List

    Application.Current.Dispatcher.BeginInvoke(DispatherPriority.Background,
        new Action(delegate { 
            obj.Items = list;
        }));
}

这篇关于NotificationObject的调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆