❔ ✅ I feel like I've gotten lost in what I'm doing right now, pls help.

public async Task DisplayUpdatedTicketsAsync(){
await UpdateTicketsAsync();
DisplayTickets(); //exception thrown here because only UI Thread may access stuff from UI
}
public async Task DisplayUpdatedTicketsAsync(){
await UpdateTicketsAsync();
DisplayTickets(); //exception thrown here because only UI Thread may access stuff from UI
}
Now this is the first time I ever experienced thread synchronization issues and I'm a little lost on what to do. I found a solution but its not very conveniant so I was hoping for a different one:
public async Task DisplayUpdatedTicketsAsync(){
await UpdateTicketsAsync();
Dispatcher.Invoke(() => { DisplayTickets(); }); //here we say wait until thread XY is done with updating Tickets before we let UI thread display the updated tickets
}
public async Task DisplayUpdatedTicketsAsync(){
await UpdateTicketsAsync();
Dispatcher.Invoke(() => { DisplayTickets(); }); //here we say wait until thread XY is done with updating Tickets before we let UI thread display the updated tickets
}
6 Replies
Pobiega
Pobiega2y ago
Why are you unhappy with this? That's literally the solution
Florian Voß
Florian VoßOP2y ago
I was able to fix the synchronization issues by doing this but I'd rather write my code without having those synchronization issues in the first place. Is there anything else I could do so that I don't have two threads trying to access the same object at the same time? I feel I've done something bad that causes this synchronizing to be necessary
Jimmacle
Jimmacle2y ago
i don't see a problem with it, the solution just explicitly schedules the code to run on the UI thread
Pobiega
Pobiega2y ago
This is what async UI code looks like I see no issues here
Florian Voß
Florian VoßOP2y ago
awesome, then I was worried for no reason. Thx guys actually I'm still unhappy with this solution and now I can express better why tho. The reason is I shouldnt be needing to use the Dispatcher here. await schedules the continuation on the calling thread and the calilng thread should be the ui thread here, not a threadpool thread. when doing Console.WriteLine(SynchronizationContext.Current) right on the first line of that function before that await call it prints System.Windows.Threading.DispatcherSynchronizationContext. it prints the same context after the await call. So why would I have to explicitely use the Dispatcher after the await call when the context before and after the await call is the Dispatcher? The await keyword should pass the continuation to the Dispatcher already thats what confuses me. I have have the same issue in other methods throughout my program @pobiega @jimmacle I have lots of seemingly unnecessary Dispatcher.Invoke() calls which are required to avoid exception for some reason
Console.WriteLine(SynchronizationContext.Current) //prints DispatcherSynchronizationContext
await DoSomething();
Console.WriteLine(SynchronizationContext.Current) //prints DispatcherSynchronizationContext
//even tho Dispatcher / UI thread has control here, any following code here must be wrapped in Dispatcher.Invoke() or I'll get said exception
Console.WriteLine(SynchronizationContext.Current) //prints DispatcherSynchronizationContext
await DoSomething();
Console.WriteLine(SynchronizationContext.Current) //prints DispatcherSynchronizationContext
//even tho Dispatcher / UI thread has control here, any following code here must be wrapped in Dispatcher.Invoke() or I'll get said exception
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server