Question: My code looks like this:

```
    public void Init() {
        if (AS.pti == PTI.UserInput)
        {
            AS.runCardTimer = false;
        }
        else
        {
            AS.runCardTimer = true;
            Device.BeginInvokeOnMainThread(() => showCards().ContinueWith((arg) => { }));
        }
    }

```

The Init method is called from the constructor. Can someone please explain to me why the developer might have added the Device.BeginInvokeOnMainThread() instead of just calling the method showCards?

Also what does the ContinueWith((arg)) do and why would that be included?

Answer: 
The class where this `Init()` method is might be created on a background thread. I'm assuming `showCards()` are updating some kind of UI. UI can only be updated on the UI/Main thread. `Device.BeginInvokeOnMainThread()` ensures that the code inside the lambda is executed on the main thread.

`ContinueWith()` is a method which can be found on `Task`. If `showCards()` returns a task, `ContinueWith()` makes sure the task will complete before exiting the lambda.
**The simple answer is:** Background thread cannot modify UI elements because most UI operations in iOS and Android are not thread-safe; therefore, you need to invoke UI thread to execute the code that modifies UI such MyLabel.Text="New Text".

**The detailed answer** can be found in Xamarin document:

For iOS:

IOSPlatformServices.BeginInvokeOnMainThread() Method simply calls NSRunLoop.Main.BeginInvokeOnMainThread

```
public void BeginInvokeOnMainThread(Action action)
{
    NSRunLoop.Main.BeginInvokeOnMainThread(action.Invoke);
}

```

<https://developer.xamarin.com/api/member/Foundation.NSObject.BeginInvokeOnMainThread/p/ObjCRuntime.Selector/Foundation.NSObject/>

> 
> You use this method from a thread to invoke the code in the specified object that is exposed with the specified selector in the UI thread. **This is required for most operations that affect UIKit or AppKit as neither one of those APIs is thread safe.**
> 
> 
> The code is executed when the main thread goes back to its main loop for processing events.
> 
> 
> 

For Android:

Many People think on Xamarin.Android BeginInvokeOnMainThread() method use Activity.runOnUiThread(), BUT this is NOT the case, and there is a difference between using runOnUiThread() and Handler.Post():

```
public final void runOnUiThread(Runnable action) {
    if (Thread.currentThread() != mUiThread) {
        mHandler.post(action);//<-- post message delays action until UI thread is scheduled to handle messages
    } else {
        action.run();//<--action is executed immediately if current running thread is UI thread. 
    }
}

```

The actual implementation of Xamarin.Android BeginInvokeOnMainThread() method can be found in AndroidPlatformServices.cs class

```
public void BeginInvokeOnMainThread(Action action)
{
    if (s_handler == null || s_handler.Looper != Looper.MainLooper)
    {
        s_handler = new Handler(Looper.MainLooper);
    }

    s_handler.Post(action);
}

```

<https://developer.android.com/reference/android/os/Handler.html#post(java.lang.Runnable)>
As you can see, you action code is not executed immediately by Handler.Post(action). It is added to the Looper's message queue, and is handled when the UI thread's scheduled to handle its message.