Use WPF Dispatcher to invoke event handler only when needed

Updated on Saturday, September 29, 2018

Use WPF Dispatcher to invoke event handler only when needed

After floundering a bit with the WPF Dispatcher I've come up with a simple way to make sure an event handler executes on the UI thread without paying the overhead of always invoking a delegate.

This has the benefit (for me at least) of being very easy to remember. Hook up the event handler and then if there's a chance it could be called from a different thread wrap it using the pattern above. It's easier to read than an anonymous delegate and much faster than defining a specific delegate for the event in question.

I haven't tested the various methods to see which is the fastest yet… will get round to this at some point.

(Related: Send event parameters with every event and multiple tags in Google Analytics 4; Export Google Fit Daily Steps, Weight and Distance to a Google Sheet; ESRI Shapefile Reader in .NET)

(You might also like: Cause and Effect, or Strange Skies over San Francisco; Chromecast won't connect to wifi - finally found the fix; Point Reyes Deer)

(All Code Posts)

Add Comment

All comments are moderated. Your email address is used to display a Gravatar and optionally for notification of new comments and to sign up for the newsletter.

I Thought He Came With You is Robert Ellison's blog.

Newsletter

Related

gtag