In Windows Forms, all controls had an InvokeRequired
property that returned true if the current thread was not the UI thread. This allowed a boilerplate pattern for ensuring a method that could be called from multiple contexts was always called from the right one:
if (someControl.InvokeRequired) {
someControl.Invoke(() => ThisMethod());
} else {
// do stuff
}
I know in Xamarin.Forms I can use Device.BeginInvokeOnMainThread()
to marshall a call to the main thread, but is there a way to detect if I'm on the main thread? That would be helpful for creating classes that represent things with thread affinity and want to enforce it.