Bug 7759 - Calling InvokeOnMainThread causes the app to hang
Summary: Calling InvokeOnMainThread causes the app to hang
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 6.0.x
Hardware: Other Other
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-10-09 19:21 UTC by dj_technohead
Modified: 2012-10-09 22:31 UTC (History)
1 user (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 dj_technohead 2012-10-09 19:21:36 UTC
I have the following code:

public void HideProgressIndicator()
            if (this.progressHUD != null)
                if (!IsMainThread())

        protected static bool IsMainThread() {
            return Messaging.bool_objc_msgSend(GetClassHandle("NSThread"),
new Selector("isMainThread").Handle);

        protected static IntPtr GetClassHandle (string clsName)
            return (new Class(clsName)).Handle;

It is hanging 100% of the time on the line where InvokeOnMainThread is being called, it is never making it to the line this.progressHUD.Hide(true). 

This seems like a problem with nested async event handling. To give a bit more context to the problem, the app that I am working on is doing the following:

Scan barcode, on scan event, show UIActivityIndicator, add http request to a queue, pull http request from queue in a thread and send http request for barcode lookup asynchronously, on completion of async http request hide UIActivityIndicator. Trying to track the code is pretty hard due to all events/asynchronous handlers, so sometimes not surprised that MT appears to be having issues.

This seems like a pretty bad problem as I don't know of a workaround and it is causing the app to completely hang, only recourse is to terminate the app.
Comment 1 dj_technohead 2012-10-09 22:31:52 UTC
I believe I found the problem. Within the code, some threads were coded to wait until a result is returned, to make an async method behave like a sync method. Removing the waiting for a result appears to have resolved this issue.