c# - 在 MonoTouch .NET 线程中使用 NSTimer 的问题

标签 c# iphone timer xamarin.ios nstimer

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

关于c# - 在 MonoTouch .NET 线程中使用 NSTimer 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4737889/

相关文章:

c# - wpf getter 和 setter

ios - iphone 上的 CSS 输出与 PC 不同

在 C 中的线程内定期调用函数

c# - 如何为个人用户创建登录 session 计时器

c# - Linq 语句需要优化

c# - 用于 C# 的 SemWeb 库 RDF 解析器

iphone - 如何使用数据包嗅探器确定应用程序是否使用 HTTP Live Streaming?

iphone - 具有 playerduel 功能的应用程序不会收到推送通知

android - 在定时器 Android 上运行新线程

c# - 使用 SQLite 进行可靠的插入