you are calling a UIKit method that can only be invoked from the UI thread.

mono touch开发时长遇到这样的错误: you are calling a UIKit method that can only be invoked from the UI thread.

这是因为,你的代码涉及到MonoTouch.UIKit库中对象,必须通过UI主线程InvokeOnMainThread来处理才行,类似下面的代码段:

using(var pool = new NSAutoreleasePool()) {
    this.InvokeOnMainThread(delegate {
        //...         
    });
}

有时候在并行编程中,通过ui主线程来处理并不能够奏效,这时候需要通过禁用UI线程,这样就不会出错:

var previous = UIApplication.CheckForIllegalCrossThreadCalls;
UIApplication.CheckForIllegalCrossThreadCalls = false;
//....
UIApplication.CheckForIllegalCrossThreadCalls = previous;
注意哦,在mono touch中,UI主线程检查是可选的,并且可以被禁用,无论是全局或局部。

你可能感兴趣的:(多线程,并行编程,UIApplication)