Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Is there a way to determine if you are on the Main thread?

$
0
0

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.


Viewing all articles
Browse latest Browse all 91519

Trending Articles