簡體   English   中英

如何在Windows Phone 7的C#中延遲方法?

[英]How to delay a method in C# for Windows Phone 7?

我有辦法

public bool bBIntersectsBT(Rect barTopTipRect, Rect barBottomTipRect, Rect blueBallRect)
    {
        barTopTipRect.Intersect(blueBallRect);
        barBottomTipRect.Intersect(blueBallRect);

        if (barTopTipRect.IsEmpty && barBottomTipRect.IsEmpty)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

我想將此方法延遲2秒,然后再次執行該方法。 我已經閱讀了有關Thread.Sleep的內容,但是,這不是我想要的。 我不希望程序暫停並恢復它。

使用DispatcherTimer

DispatcherTimer timer = new DispatcherTimer();

//TimeSpan is in format: Days, hours, minutes, seconds, milliseconds.
timer.Interval = new TimeSpan(0, 0, 0, 2);
timer.Tick += timerTick;
timer.Start();

private void timerTick(Object sender, EventArgs e)
{
    //Your code you want to execute every 2 seconds

    //If you want to stop after the two seconds just add timer.Stop() here
}

您可以使用調度計時器來實現您的目標。 需要時將其設置為2秒。 在完成后。 你可以阻止它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM