【发布时间】:2011-04-05 13:35:45
【问题描述】:
查看 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 调用 Web 服务。
我还应该做些什么来在完成时销毁线程?
应用程序在长时间后崩溃 - 我们猜测它可能是线程 - 因为它会定期启动它们以执行短期操作。我读过由于垃圾收集,在 MonoTouch 中监控内存使用是一个挑战 - 或者我会查看内存是否也被消耗(例如,剩余线程)。
我正在使用 MonoTouch 3.2.5 和 iOS 4.3.1(以及 Xcode 4 中的 Instruments)。应用需要在 iOS 3.x 上运行,所以我不能使用 Grand Central Dispatch。
【问题讨论】:
标签: c# iphone multithreading xamarin.ios