2
我遇到了一个问题,那就是在发布模式下编译时,我的设备上的大部分时间(而非全部)我的NSTimer操作未被调用。在设备上的调试模式下总是很好,在模拟器的任何一种模式下总是很好。设备上的发布模式中的间歇性NSTimer问题
对这种情况唯一有点不寻常的是,它被另一个类包裹起来,允许我们在运行时注入特定于平台的实现,例如定时器,思考向monodroid等等。尽管如此,没有什么特别的异常。
代码:
using System;
using MonoTouch.Foundation;
namespace OurCompany.PlatformAbstraction.Ios
{
public class IosTimer : IPlatformTimer
{
private NSTimer backingTimer;
public double IntervalMs{get; set;}
public IosTimer(double intervalMS)
{
this.IntervalMs = intervalMS;
}
public event EventHandler Elapsed;
private void NsTimerElapsed()
{
if (Elapsed != null)
{
Elapsed(this, EventArgs.Empty);
}
}
public void Start()
{
TimeSpan ts = TimeSpan.FromMilliseconds(this.IntervalMs);
this.backingTimer = NSTimer.CreateRepeatingScheduledTimer(ts, NsTimerElapsed);
}
public void Stop()
{
this.backingTimer.Invalidate();
this.backingTimer = null;
}
}
}
调用代码:
private IosTimer t;
public override void ViewDidLoad()
{
base.ViewDidLoad();
t = new IosTimer(100);
t.Elapsed += UpdateLabel;
t.Start();
}
private void UpdateLabel(object sender, EventArgs e)
{
// not being called in Release mode on the phone
// works fine in Debug mode on the phone, works fine in both modes in the simulator
}
我也注意到一般行为的差异 - 在一些正常工作在模拟器,但无法在设备上。由于两个完全相同的代码部署到同一个设备(!),所以也有轻微的行为变化
昨天发布在MonoTouch论坛上,但它在那里似乎有点安静。
在此先感谢。
请在http://monotouch.net/Support – 2011-03-29 15:39:45
上提供一个测试用例的错误您是否尝试过使用System.Timers.Timer?我已经使用它,并没有像你提到的任何问题。 – jonathanpeppers 2011-03-29 23:43:45
啊,辉煌 - 出于某种原因,我的印象是System.Timers.Timer在MonoTouch可用的库中不存在。更少的东西要抽象... – tomfanning 2011-03-30 09:56:23