I'm having a problem with using multiple UI dispatchers to modify an list that is bound to the UI. The method with just exit when it hits the first dispatcher. If I wrap the entire method in the dispatcher it works, but I have another solution but I'm not sure it's appropriate:
Basically, I have a socket listening in a never-ending loop for network commands from a media device. When it finds one, it calls ProcessCommand.
That function calls one of 50+ methods to process the specific commands. Those functions store the internal state, but mainly raise events my main application can subscribe to so it knows when something like the volume has changed and I can update the UI.
These work pretty well, except the one case noted at the start where I need to modify a state object that is bound using several dispatchers in the same method and it not working, and wrapping the method in one big dispatcher seems to work.
Another solution I found is to run the ProcessCommand running in the socket background listener on the UI dispatcher, ie:
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
ProcessCommand(command);
});
This will then trickle down into all of the individual 50+ ProcessCommandXYZ methods, and also in my main app where I subscribe to these events on multiple pages I currently have to use a UI dispatcher on every one, so something like:
Socket listener background task > {DispatcherUI ProcessCommand} > ProcessVolumeCommand > Raise OnVolumeChangedEvent > Subscriber updates UI
Would make it so I don't need to put each event subscriber into a dispatcher, but most importantly fixes problem I have with multiple dispatchers in one method:
Using Dispatcher correctly in event to update UI
Does this seem like a good solution?
Here is my solution for such situations when you subscribed to event that raises on Non-ui thread.
And here is implementation: