Bug 13411 - It is possible to make UI calls from a non GUI thread
Summary: It is possible to make UI calls from a non GUI thread
Status: RESOLVED FEATURE
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 6.9.3.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-07-23 10:26 UTC by Zhenya
Modified: 2013-07-23 23:38 UTC (History)
3 users (show)

Tags:
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:
Status:
RESOLVED FEATURE

Description Zhenya 2013-07-23 10:26:01 UTC
I was accidentally calling UIView.SetNeedsLayout from a non GUI thread (from a Timer event handler). Understandably, the code never worked. It took me awhile to realize that calling thread is not main. Would be nice to have a warning in application output about making a GUI call from a thread different from main.
Comment 1 Sebastien Pouliot 2013-07-23 14:26:33 UTC
We already have this feature, see: http://tirania.org/monomac/archive/2012/Sep-10.html

This should be, by default, enabled in your debug builds. It won't be (again by default) enabled in release builds since it can have a noticeable performance impact for some code.

If you run into a case where you think you should have got this exception, but did not, then please file (or re-open) this bug report and include a test case so we can duplicate and reproduce the issue.
Comment 2 Zhenya 2013-07-23 20:08:06 UTC
Thanks for the tip! It doesn't look like --force-thread-check option (is it the right one?) is enabled in the Debug configuration of my project. I tried to add the option manually (Build Options, General, iOS Build, Additional touch arguments) but unfortunately it didn't have any effect. 

My code base is rather big, so I can't really extract a test case. But in a nutshell, I was trying to call UIView.SetNeedsDisplay from a System.Timers.Timer.Elapsed event handler. Not sure if it matters, but Timer was created in iOS library. And call to SetNeedsDisplay happened in iOS Application project.
Comment 3 Sebastien Pouliot 2013-07-23 20:50:24 UTC
You do not have to enable it - it's ON, by default,for debug build (but you can turn it off) and OFF, by default, for release (but you can turn it on).

The real issue is your choice of Timer. By design, System.Timers.Timer.Elapsed won't report the exception that monotouch.dll (or your own code) is throwing inside the event. MSDN quote [1]:

> The Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event.

So in reality it did *not* work because the exception was thrown and the actual `SetNeedsDisplay` was never executed, nor would any code after it (try to add your own Console.WriteLine after SetNeedsDisplay and it will never be seen).

That's different from what generally happens: i.e. it works some times, does not work some times and crash (not often enough to catch while testing, too many times once deployed).

You can easily confirm this by configuring Xamarin Studio to stop on every exceptions (or specifically on UIKitThreadAccessException).

An easy way to solve this (because other exceptions could happen in your `Elapsed` event) would be to use the native timer from iOS, NSTimer.

[1] http://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed.aspx
Comment 4 Zhenya 2013-07-23 23:38:44 UTC
Oh, I see. That makes total sense. I'd really like to keep my library platform independent so I guess I'll just instruct debugger to stop on exceptions I need. Thanks!