Threading

From $1
Table of contents

It is possible to use multiple threads in MonoTouch applications, either directly when using System.Threading.Thread or the System.Threading.ThreadPool or indirectly when using the various asynchronous methods exposed by the API (The Begin/End pattern for delegates and APIs).

Access to UI elements should be limited to the same thread that is running the main loop for your application.    If you want to make changes to the main UI from a thread, you should queue the code by using NSObject.InvokeOnMainThread, like this:

MyThreadedRoutine ()
{
    var result = DoComputation ();
    InvokeOnMainThread (delegate {
        label.Text = "The result is: " + result;
    });
}

InvokeOnMainThread will suspend the execution of your thread, and wait for the delegate to be executed on the main thread. When that completes, the thread resumes execution. Additionally, it is possible to use InvokeOnMainThreadAsync() to queue a delegate for invocation without pausing the calling thread.

Tag page

Files (0)

 
Page last modified 05:44, 3 Nov 2009 by Miguel