Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Wait for Android runOnUiThread to finished - emulate Invoke instead of BeginInvoke

I want to replicate the Invoke, instead of the BeginInvoke behavior, when I am executing an action in the UI Thread.
Java wise this is implemented like this
while in WPF it is implemented like this:

        Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, action);

My approach is using AutoResetEvent, in order to emulate the above Java approach with C#

public void Invoke(Action action)
EventWaitHandle _autoResetEvent=new AutoResetEvent(false);
var lockObj = new Object();
var handler = new Handler(Looper.MainLooper);
Log.Info(TrustVillageBack.ResourceRoot, ".MultiDebug: " + "Invoke was called for action: "+action.ToString());

       lock (lockObj)
           if (handler != null)

               //We are not in the main Looper.
               // If we were, then the main thread would be blocked
               // be waitone
               if (Looper.MyLooper() != Looper.MainLooper)

                       Log.Info(TrustVillageBack.ResourceRoot, ".MultiDebug: " + "Invoke was not on main looper and autoResetEvent is triggered " + action.ToString());


               throw new ArgumentNullException("Handler in Service is Null");


however, after repeated tests this does not work, Invoke is not blocking until action is executed. Any ideas what I am doing wrong here?
Thanks in advance

Sign In or Register to comment.