Bug 7347 - NSThread, UI thread safety
Summary: NSThread, UI thread safety
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 5.4.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
Depends on:
Reported: 2012-09-20 13:00 UTC by James Clancey
Modified: 2012-09-20 20:08 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description James Clancey 2012-09-20 13:00:07 UTC
NSThread is missing a binding for isMainThread.

Also the following should be added the UIKit thread safety whitelist.

private void EnsureInvokedOnMainThread (Action action)
			if (IsMainThread ())
				action ();
			this.BeginInvokeOnMainThread (() => action());
		private static IntPtr GetClassHandle (string clsName)
			return (new Class(clsName)).Handle;
		private static bool IsMainThread() {
			return Messaging.bool_objc_msgSend(GetClassHandle("NSThread"), new Selector("isMainThread").Handle);
Comment 1 Sebastien Pouliot 2012-09-20 15:48:51 UTC
it seems only the instance '-' `isMainThread` has been bound (not the static '+' one).
Comment 2 Sebastien Pouliot 2012-09-20 16:15:21 UTC
That introduce C# limitations wrt to the names, I'll try to work something (renaming).

OTOH your code would be simpler (today) by doing:

if (NSThread.Current.IsMainThread)
Comment 3 James Clancey 2012-09-20 16:17:17 UTC
I will switch to NSThread.Current.IsMainThread. That should be UIKit thread safe right?  ITs not the other way I was doing it.
Comment 4 Sebastien Pouliot 2012-09-20 16:22:44 UTC
The thread check should only occurs on types inside the MonoTouch.UIKit namespace that are not decorated with [ThreadSafe] in their bindings.

OTOH I'm not sure, from your description, what was the code that would trigger that ?!? can you copy/paste the full exception (w/stack trace) ? thanks
Comment 5 James Clancey 2012-09-20 16:28:00 UTC
I was getting the UIKit Internal consistency error. But I cant get it to happen anymore.
Comment 6 Sebastien Pouliot 2012-09-20 16:33:48 UTC
"UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread."

That's our message ^

No clue on how you got it with the above code ?!? If it ever occurs again try to get the stack trace out of it (if possible).
Comment 7 Sebastien Pouliot 2012-09-20 20:08:06 UTC
Fixed in b3f7f3f09535d9bef669a239171464d8f9ddc7fe

If you get the exception again please re-open w/details