简体   繁体   English

如何在 Monotouch Button TouchDown 事件处理程序中的 while 循环中休眠执行

[英]How to sleep execution in while cycle in Monotouch Button TouchDown event handler

I tried Thread.Sleep(1000) in every iteration in cycle, but infoLabel.Text is not changed until end of cycle.It changes infoLabel.Text after calling Thread.Sleep 3 times.我在循环中的每次迭代中都尝试了Thread.Sleep(1000) ,但infoLabel.Text直到循环结束才更改。它在调用 Thread.Sleep 3 次后更改了 infoLabel.Text。 Please advice.请指教。 Here's my code.这是我的代码。

void Handle_Touchdown(object sender, EventArgs e)
{
 for(int i = 0; i < 10; ++i )
 {
    infoLabel.Text = i.ToStraing();
    Thread.Sleep(1000);
 }
}

You're probably better off implementing a NSTimer and incrementing a count.你可能最好实现一个NSTimer并增加一个计数。 Possible code could look like:可能的代码如下所示:

void Handle_Touchdown(object sender, EventArgs e)
{
    NSTimer timer;
    int counter = 0;

    timer = NSTimer.CreateRepeatingScheduledTimer(1, delegate{
        if (counter != 10)
        {
            counter++;
            infoLabel.Text = i.ToString();
        }
        else
            timer.Invalidate() // stop the timer
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM