Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MonoTouch.NET线程中使用NSTimer时出现问题_C#_Iphone_Timer_Xamarin.ios_Nstimer - Fatal编程技术网

C# 在MonoTouch.NET线程中使用NSTimer时出现问题

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

我在MonoTouch中使用NSTimer时遇到问题。首先,我在主线程中运行了一个NSTimer,这很有效。然而,我已经将计时器移动到了一个单独的线程,并且我从未收到回调。我猜这是因为我的.NET风格的线程不是运行循环——但我对MonoTouch/iOS还很陌生,所以不确定

我已经将代码提取到一个测试项目中,遇到了同样的问题。这是失败的代码:

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);

太好了,非常感谢你的帮助。这非常有效。