检测您何时进入/离开Xamarin.iOS中的主线程

Dav*_*les 22 multithreading xamarin.ios ios

在Xamarin/MonoTouch中是否有办法检测是否在主线程中调用了代码?

我正在寻找类似于Java的东西EventQueue.isEventDispatchThread()- 我在Swing编程中发现它assert不时(或有时候assert不是这样)很方便 - 确保模型不断更新并从EDT中读取,长时间运行的调用不会阻止UI.

我想在我的MonoTouch应用程序中做同样的事情,以确保不会从UI调用或包含各种代码InvokeOnMainThread.


更新:对于后来出现的人:来自JP的Obj-C回答如下.Xamarin/MonoTouch相当于NSThread.Current.IsMainThread.

Jay*_*yer 41

我对Monotouch了解不多,但在iOS中+[NSThread isMainThread]可能就是你想要的.

偶尔在编写多线程代码时,我会输入一个这样的断言:

NSAssert([NSThread isMainThread], @"Method called using a thread other than main!");
Run Code Online (Sandbox Code Playgroud)