查看 Instruments 中的 Time Profiler 工具,看起来我在使用线程后并没有处理它们。 “所有线程”列表随着我执行启动线程的操作而增长。
我按如下方式启动我的线程(“线程”对函数而言是本地的):
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
Thread thread = new Thread(Dispatch);
thread.Start();
Dispatch() 函数是:
private void Dispatch()
{
using (NSAutoreleasePool autoreleasePool = new NSAutoreleasePool())
{
try
{
// Calls a web service.
_workDispatcher.Dispatch();
}
finally
{
// Resets network activity indicator after thread complete.
InvokeOnMainThread(() => { UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false; });
}
}
}
_workerDispatcher.Dispatch 调用网络服务。
我还应该做些什么来在完成时销毁线程?
应用程序在很长一段时间后崩溃 - 我们猜测它可能是线程 - 因为它定期启动它们以执行短期操作。我已经读到,由于垃圾收集,监控 MonoTouch 中的内存使用情况是一项挑战 - 或者我会查看内存是否也被消耗(例如,剩余线程)。
我使用的是 MonoTouch 3.2.5 和 iOS 4.3.1(以及 Xcode 4 中的 Instruments)。应用程序需要在 iOS 3.x 上运行,所以我无法使用 Grand Central Dispatch。
最佳答案
你确定这些是你的线程吗? MonoTouch 更有可能为将来的异步操作保留一些线程池线程(比如调用 Web 服务中的线程)。
关于c# - 如何清理 MonoTouch 中的线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5552709/