检测您何时进入/退出Xamarin.iOS中的主线程 [英] Detecting when you're in/out of the main thread in Xamarin.iOS

查看:320
本文介绍了检测您何时进入/退出Xamarin.iOS中的主线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way in Xamarin/MonoTouch to detect whether code is being called in the main thread?

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

I'm looking for something like the equivalent of Java's EventQueue.isEventDispatchThread() -- I've found in Swing programming it's handy to assert that from time to time (or sometimes to assert that it's not) -- making sure that models are consistently updated and read from the EDT, and that long-running calls don't block the UI.

我想在我的MonoTouch应用程序中执行相同操作,以确保不会从UI调用各种代码,或者包含在<$ c $中c> InvokeOnMainThread 。

I'd like to do the same in my MonoTouch app, to ensure that various bits of code are/aren't called from the UI, or wrapped in InvokeOnMainThread.

更新:对于任何人来之后: Obj-C 从以下JP回答。 Xamarin / MonoTouch相当于 NSThread.Current.IsMainThread

Updated: For anyone coming along later: Obj-C answer from JP below . The Xamarin/MonoTouch equivalent is NSThread.Current.IsMainThread.

推荐答案

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

I don't know much about Monotouch, but in iOS +[NSThread isMainThread] might be what you're looking for.

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

Occasionally when writing multithreaded code, I will put in an assert like this:

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

这篇关于检测您何时进入/退出Xamarin.iOS中的主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆