❔ ✅ I feel like I've gotten lost in what I'm doing right now, pls help.
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:
6 Replies
Why are you unhappy with this?
That's literally the solution
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
i don't see a problem with it, the solution just explicitly schedules the code to run on the UI thread
This is what async UI code looks like
I see no issues here
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
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.