You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've got a .NET desktop app that needs to update its UI when an event has been consumed. It's not a surprise that Consume gets called from a different thread. Is there a recommended way to handle this scenario? Right now I'm registering the main window's Dispatcher into DI and injecting it into the type that implements IConsumer<T>. It works fine but the issue comes when the desktop app is starting up and there are messages in the queue to process. I was starting the bus before the main window was shown which would cause the thread dispatcher to be null and cause issues.
Is there a recommended way to handle this? Right now I'm hooked into the main window's Activated event and starting the bus at that point. It works but feels like there is a better way to do this. Here is what my consumer looks like right now.
publicclassJobEventsConsumer:IConsumer<JobAssigned>{privatereadonlyIMessenger_messenger;// This is coming from CommunityToolkit.Mvvm.MessagingprivatereadonlyDispatcher_uiThread;publicJobEventsConsumer(IMessengermessenger,DispatcheruiThread)=>(_messenger, _uiThread)=(messenger, uiThread);public Task Consume(ConsumeContext<JobAssigned>context){// I didn't want to have a dependency on the view model from this perspective and thought that doing some sort of event// aggregation would be cleaner. I'm definitely open to other ideas.
_uiThread.Invoke(()=> _messenger.Send(context.Message));return Task.CompletedTask;}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I've got a .NET desktop app that needs to update its UI when an event has been consumed. It's not a surprise that
Consume
gets called from a different thread. Is there a recommended way to handle this scenario? Right now I'm registering the main window'sDispatcher
into DI and injecting it into the type that implementsIConsumer<T>
. It works fine but the issue comes when the desktop app is starting up and there are messages in the queue to process. I was starting the bus before the main window was shown which would cause the thread dispatcher to benull
and cause issues.Is there a recommended way to handle this? Right now I'm hooked into the main window's
Activated
event and starting the bus at that point. It works but feels like there is a better way to do this. Here is what my consumer looks like right now.Beta Was this translation helpful? Give feedback.
All reactions