This is from a Microsoft WPF example. The source can be found on git as UsingDispatcher.
It is included zip file that has a lot of examples.
It is sort of written up hear. I have seen it in other places on MSDN. https://docs.microsoft.com/en-us/dotnet/desktop/wpf/advanced/threading-model?view=netframeworkdesktop-4.8&viewFallbackFrom=netdesktop-5.0.
I added the commented out line below the error. It works OK. Still, why the error?