iOS Guides | Samples

MonoTouch.Foundation.NSObject.BeginInvokeOnMainThread Method

Invokes asynchrously the specified code on the main UI thread.

Syntax

public void BeginInvokeOnMainThread (NSAction action)

Parameters

action
The code to invoke on the main thread.

Remarks

You use this method from a thread to invoke the code encapsulated by the NSAction delegate 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.

Unlike NSObject.InvokeOnMainThread(NSAction) this method merely queues the invocation and returns immediately to the caller.

c# Example

// Invoked in response to a button clicked on the UI.
void StartDownload ()
{
	status.Text = "Downloading file...";
	ThreadPool.QueueUserWorkItem (BackgroundDownload);
}
// This is the background helper
BackgroundDownload ()
{
	var wc = new WebClient ();
	wc.DownloadFile (myUrl, "/tmp/largeFile.txt");
	// When the download is complete, notify the UI
	BeginInvokeOnMainThread (delegate {
		status.Text = "Download has been completed";
	});
}

Requirements

Namespace: MonoTouch.Foundation
Assembly: monotouch (in monotouch.dll)
Assembly Versions: 0.0.0.0