Every time I had to update the ui from a non-ui thread I always used
the Application.Current.Dispatcher and never paid much attention to it.
Just call a begin invoke from the other thread and you are done. It was
only recently that I realized that that’s not the only way of doing
things. There is an alternative way using the ‘SynchronizationContext’
which also gets the job done. So what’s the difference?
I couldn’t figure out anything obvious so I started reading up articles on it and finally found a comprehensive article here.
So basically SynchronizationContext adds another level of
indirection. Unlike the dispatcher which is WPF specific, the
SynchronizationContext has different implementations – WPF and
WindowsForms being some of them. So, now your code is not dependent on
any technology which is always great.
Though in theory the synchronization context can be used to
communicate between any two threads, not all threads have a
synchronization context. Only the UI thread has the required context and
the plumbing code required for getting it to work.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.