该应用程序调用了一个接口,该接口在使用MVVM light Messenger时被编组为不同的线程 [英] The application called an interface that was marshalled for a different thread when using MVVM light messenger

查看:183
本文介绍了该应用程序调用了一个接口,该接口在使用MVVM light Messenger时被编组为不同的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公共计时器,在其Tick事件中,我向ViewModel发送了一条消息.我从应用程序中某个位置的按钮启动计时器.问题是,当ViewModel尝试注册(使用MVVM Light)时,存在异常:

I have a public timer and in its Tick event I send a message to my ViewModel. I start the timer from a button somewhere in my application. The problem is there is an exception when the ViewModel tries to register (using MVVM Light):

该应用程序调用了已编组为另一个线程的接口.(HRESULT的异常:0x8001010E(RPC_E_WRONG_THREAD))"

这是计时器

public static class SomeManager
{
    private static Timer gAppTimer;
    private static object lockObject = new object();

    public static void StartTimer()
    {
        if (gAppTimer == null)
        {
            lock (lockObject)
            {
                if (gAppTimer == null)
                {
                    gAppTimer = new Timer(OnTimerTick, null, 10000, 10000);
                }
            }
        }
    }

    public static void StopTimer()
    {
        if (gAppTimer != null)
        {
            lock (lockObject)
            {
                if (gAppTimer != null)
                {
                    gAppTimer.Change(Timeout.Infinite, Timeout.Infinite);
                    gAppTimer = null;
                }
            }
        }
    }

    private static void OnTimerTick(object state)
    {
        Action();
    }

    public static void Action()
    {
        GlobalDeclarations.GlobalDataSource.Clear();

        Messenger.Default.Send<ObservableCollection<PersonVMWrapper>>(GlobalDeclarations.GlobalDataSource);
    }
}

这是ViewModel:

public class PersonVM : INotifyPropertyChanged
{
    ....

    public PersonVM()
    {
        Messenger.Default.Register<ObservableCollection<PersonVMWrapper>>
        (
            this,
            (action) => ReceiveMessage(action)
        );
    }

    private void ReceiveMessage(ObservableCollection<PersonVMWrapper> action)
    {
        foreach (PersonVMWrapper pvmw in action)
        {
            DataSource.Add(pvmw);
        }
    }

    ...

推荐答案

如果将ObservableCollection绑定到控件,则只能从UI线程中添加或删除值.

If your ObservableCollection is bound to a control, you can add or remove values only from the UI thread.

为此,您可以使用MVVM Light Toolkit提供的DispatcherHelper类:

For this, you can use the DispatcherHelper class provided by the MVVM Light Toolkit:

DispatcherHelper.CheckBeginInvokeOnUI(
    () =>
    {
        foreach (PersonVMWrapper pvmw in action)
        { 
            DataSource.Add(pvmw);
        }
    });

还可以直接调用调度程序,而无需使用MVVM Light Toolkit:

It's also possible to call directly the dispatcher without using the MVVM Light Toolkit:

Deployment.Current.Dispatcher.BeginInvoke(
    () =>
    {
        foreach (PersonVMWrapper pvmw in action)
        { 
            DataSource.Add(pvmw);
        }
    });

这篇关于该应用程序调用了一个接口,该接口在使用MVVM light Messenger时被编组为不同的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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