سؤال

I have two classes lets say ClassA and ClassB. I have to communicate with the help of Galasoft Messenger class. In ClassA, I have the following code :-

Class A :

Messenger.Default.Register<NotifyInterface>(this, PerformAction);

private void PerformAction(NotifyInterface objNotify)
{
   switch(objNotify.SomeProperty)
   {
    case "A":
       // Do Something
       break;
    case "B":
       // Do something else
       break;
    }
}

From ClassB I want to send parallel notifications with the help of Messenger i.e. I want to execute 'case A' and 'case B' parallely. I am doing something like this.

ClassB:

System.Threading.ThreadPool.QueueUserWorkItem((object context) =>
        {
            Messenger.Default.Send<NotifyInterface>(objNotifyCaseA);                
        }, null);

Messenger.Default.Send<NotifyInterface>(objNotifyCaseB);

But the second notification is not received until the first one gets over. Is this a limitation of Messenger class that two notifications cannot be sent in parallel?

هل كانت مفيدة؟

المحلول

Well, not sure whether it is a bug or by-design, but yes - messenger locks while sending messages (for instance, if you'll try to send a message from within another message's handler you'll get a dead-lock).

The issue was discussed at mvvm light toolkit project site and the solution was proposed as this pull request, but afaik it is not yet confirmed and merged into trunk.

As a temporary workaround until a new version of toolkit is released you can get sources and apply changes mentioned above - those will solve the issue.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top