简体   繁体   中英

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

I have a method.

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

I want to delay this method for 2 seconds before this method is executed again. I have read up about Thread.Sleep, However, that is not what I want. I do not want the program to pause and resume it.

Use a 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
}

You can user Dispatch Timer to achieve your goal. Set it to 2 seconds when you want. And after you are done with it. you can stop it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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