设备上 Release 模式下间歇性 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 论坛上发布了此内容,但那里似乎有点安静。
提前致谢。
I am having a problem where most, but not all, of the time on my device my NSTimer action is not being called when compiled in Release mode. It's always fine in Debug mode on the device, and it's always fine in either mode in the simulator.
The only thing that's slightly unusual about this situation is that it's wrapped with another class to allow us to inject platform-specific implementations of things like timers at runtime, thinking forwards to monodroid etc. Nothing particularly unusual going on though.
Code:
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;
}
}
}
Calling code:
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
}
I'm also noticing general behaviour differences - where something works fine in the simulator but not on the device. Also slight behaviour changes given two identical deployments of the exact same code to the same device(!)
Posted this yesterday on the MonoTouch forums but it seems a bit quiet over there.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明这是与线程相关的。通过在我的 Elapsed 事件处理程序中使用 InvokeOnMainThread() 来解决。
Transpired this was threading related. Solved by using InvokeOnMainThread() in my Elapsed event handlers.