C# 如何在Monotouch按钮触地事件处理程序的while循环中休眠执行

C# 如何在Monotouch按钮触地事件处理程序的while循环中休眠执行,c#,multithreading,event-handling,xamarin.ios,uibutton,C#,Multithreading,Event Handling,Xamarin.ios,Uibutton,我在循环中的每个迭代中都尝试了Thread.Sleep(1000),但是infoLabel.Text直到循环结束才更改。它在调用Thread.Sleep 3次后更改infoLabel.Text。 请给我一些建议。这是我的密码 void Handle_Touchdown(object sender, EventArgs e) { for(int i = 0; i < 10; ++i ) { infoLabel.Text = i.ToStraing(); Thread.Sl

我在循环中的每个迭代中都尝试了
Thread.Sleep(1000)
,但是
infoLabel.Text
直到循环结束才更改。它在调用Thread.Sleep 3次后更改infoLabel.Text。 请给我一些建议。这是我的密码

void Handle_Touchdown(object sender, EventArgs e)
{
 for(int i = 0; i < 10; ++i )
 {
    infoLabel.Text = i.ToStraing();
    Thread.Sleep(1000);
 }
}
void Handle\u着陆(对象发送方,事件参数e)
{
对于(int i=0;i<10;++i)
{
infoLabel.Text=i.tostraining();
睡眠(1000);
}
}

您最好实现一个
NSTimer
并增加一个计数。可能的代码如下所示:

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

您最好实现一个
NSTimer
并增加一个计数。可能的代码如下所示:

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