C# 在MonoTouch.NET线程中使用NSTimer时出现问题
我在MonoTouch中使用NSTimer时遇到问题。首先,我在主线程中运行了一个NSTimer,这很有效。然而,我已经将计时器移动到了一个单独的线程,并且我从未收到回调。我猜这是因为我的.NET风格的线程不是运行循环——但我对MonoTouch/iOS还很陌生,所以不确定 我已经将代码提取到一个测试项目中,遇到了同样的问题。这是失败的代码:C# 在MonoTouch.NET线程中使用NSTimer时出现问题,c#,iphone,timer,xamarin.ios,nstimer,C#,Iphone,Timer,Xamarin.ios,Nstimer,我在MonoTouch中使用NSTimer时遇到问题。首先,我在主线程中运行了一个NSTimer,这很有效。然而,我已经将计时器移动到了一个单独的线程,并且我从未收到回调。我猜这是因为我的.NET风格的线程不是运行循环——但我对MonoTouch/iOS还很陌生,所以不确定 我已经将代码提取到一个测试项目中,遇到了同样的问题。这是失败的代码: var thread=new Thread(StartTimer as ThreadStart); thread.Start(); [Export("S
var thread=new Thread(StartTimer as ThreadStart);
thread.Start();
[Export("StartTimer")]
void StartTimer()
{
using(var pool = new NSAutoreleasePool())
{
timer=NSTimer.CreateRepeatingScheduledTimer(2,delegate { Twitch() } );
Thread.Sleep(1000000); // do I have to yield here somehow?
}
}
void Twitch()
{
Debug.WriteLine("Twitch");
}
您是对的,它从未触发的问题是您已将线程置于睡眠状态,并且从未启动运行循环。尝试:
NSRunLoop.Current.Run ();
而不是你的
Thread.Sleep (1000000);
太好了,非常感谢你的帮助。这非常有效。