22

Is there a way in Xamarin/MonoTouch to detect whether code is being called in the main thread?

I'm looking for something like the equivalent of Java's EventQueue.isEventDispatchThread() -- I've found in Swing programming it's handy to assert that from time to time (or sometimes to assert that it's not) -- making sure that models are consistently updated and read from the EDT, and that long-running calls don't block the UI.

I'd like to do the same in my MonoTouch app, to ensure that various bits of code are/aren't called from the UI, or wrapped in InvokeOnMainThread.


Updated: For anyone coming along later: Obj-C answer from JP below . The Xamarin/MonoTouch equivalent is NSThread.Current.IsMainThread.

David Moles
  • 48,006
  • 27
  • 136
  • 235
  • possible duplicate of [Check whether or not the current thread is the main thread](http://stackoverflow.com/questions/3546539/check-whether-or-not-the-current-thread-is-the-main-thread) – bobobobo May 07 '13 at 20:56
  • @bobobobo That question doesn't directly address the Monotouch syntax. – David Moles May 07 '13 at 21:01
  • I didn't notice the [tag:monotouch]. Well, the answer ended up being the same. – bobobobo May 07 '13 at 21:02
  • @bobobobo I can de-accept the answer and answer it myself with the note I added at the end of the question, if that would make you happier. – David Moles May 07 '13 at 21:04

3 Answers3

41

I don't know much about Monotouch, but in iOS +[NSThread isMainThread] might be what you're looking for.

Occasionally when writing multithreaded code, I will put in an assert like this:

NSAssert([NSThread isMainThread], @"Method called using a thread other than main!");
Jay Peyer
  • 2,019
  • 1
  • 19
  • 17
1

The only problem with NSAssert([NSThread isMainThread], errorDesc) is that you BETTER be in the main thread when making this call. If you happen to be in a secondary thread and make the call then your application crashes! So it's kind of pointless.

It's better just to simply use [NSThread isMainThread] then assess the BOOL value it returns.

PostCodeism
  • 1,070
  • 1
  • 12
  • 20
  • 13
    You're correct that the NSAssert in my answer will crash the app if it isn't run from the main thread. That's the purpose of using an NSAssert. During development, I use that assert to alert me if my code ever gets executed in the a non-main thread. You don't have to use the NSAssert from my answer -- you can do anything with that function. The NSAssert code is just an example of a possible use for the +[NSThread isMainThread] function. As a side note, your answer probably could have been a comment on my answer instead of a whole separate answer. – Jay Peyer Mar 07 '11 at 17:11
0

You can do like this in Monotouch / Xamarin.ios

        if (NSThread.Current.IsMainThread)
        {
            //You are in the MainThread
        } 

This check is very useful to avoid the error that might occur when trying to modify the UI from a background Thread. Something Like this can be done:

if (NSThread.Current.IsMainThread)
{
    DoSomething();
}
else
{
    BeginInvokeOnMainThread(() => DoSomething());
}
Daniele D.
  • 2,624
  • 3
  • 36
  • 42