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

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 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>