In Framework, this is easily accomplished by including a reference to the PresentationFramework class assembly and then using something like this:
public static void UIThread( Action action )
{
if (Application.Current.Dispatcher.CheckAccess())
{
// On FG thread. Can directly invoke the action.
action.Invoke();
}
else
{
// On BG thread. Must do an invoke to get on FG.
Application.Current.Dispatcher.Invoke( action );
}
}
I'm in a Core framework and needed to know if the code in a class library was executing on a foreground thread or in the background and, if BG, then it would need to make the transition to FG. I attempted to add PresentationFramework to my class library module. Hmm. Visual Studio doesn't seem to want me to do that.
Add Reference doesn't work the same. It doesn't have the same ability to add PresentationFramework.dll to the references. Similarly, managing Nuget Packages doesn't really seem to want to insert a reference to PresentationFramework.dll.
Hours later, I've given up on discovering the secret for myself. Time to ask the community. So, how does one figure out if the current thread is FG or not in Core when in a class library DLL module?
public static void UIThread( Action action )
{
if (Application.Current.Dispatcher.CheckAccess())
{
// On FG thread. Can directly invoke the action.
action.Invoke();
}
else
{
// On BG thread. Must do an invoke to get on FG.
Application.Current.Dispatcher.Invoke( action );
}
}
I'm in a Core framework and needed to know if the code in a class library was executing on a foreground thread or in the background and, if BG, then it would need to make the transition to FG. I attempted to add PresentationFramework to my class library module. Hmm. Visual Studio doesn't seem to want me to do that.
Add Reference doesn't work the same. It doesn't have the same ability to add PresentationFramework.dll to the references. Similarly, managing Nuget Packages doesn't really seem to want to insert a reference to PresentationFramework.dll.
Hours later, I've given up on discovering the secret for myself. Time to ask the community. So, how does one figure out if the current thread is FG or not in Core when in a class library DLL module?