Tiina
Tiina

Reputation: 4797

winphone let code wait for 2 seconds DispatcherTimer

I m doing image processing for Win Phone, using visual studio 2010. In order to let a a picture displayed for 2 seconds (like slide show), the following class is called

namespace photoBar
{
    public class WaitTwoSeconds
    {
        DispatcherTimer timer = new DispatcherTimer();
        public bool timeUp = false;

        // This is the method to run when the timer is raised. 
        private void TimerEventProcessor(Object myObject,
                                                EventArgs myEventArgs)
        {
            timer.Stop();
            timeUp = true;
        }

        public WaitTwoSeconds()
        {
            /* Adds the event and the event handler for the method that will 
               process the timer event to the timer. */
            timer.Tick += new EventHandler(TimerEventProcessor);

            // Sets the timer interval to 2 seconds.
            timer.Interval = new TimeSpan(0, 0, 2); // one second
            timer.Start();

            //// Runs the timer, and raises the event. 
            while (timeUp== false)
            {
                // Processes all the events in the queue.
                Application.DoEvents();
            } 
        }
    }
}

It is called in this way:

        WaitTwoSeconds waitTimer = new WaitTwoSeconds();
        while (!waitTimer.timeUp)
        {
        }

Because the Application.DoEvents(); is claimed as an error: 'System.Windows.Application' does not contain a definition for 'DoEvents' . So I deleted that code block

        while (timeUp== false)
        {
            // Processes all the events in the queue.
            Application.DoEvents();
        } 

After compile and run the program, it shows resume ...
How could I correct this? Thanks

Upvotes: 0

Views: 706

Answers (2)

user1228
user1228

Reputation:

Embrace multithreading.

public class WaitTwoSeconds
{
    DispatcherTimer timer = new DispatcherTimer();
    Action _onComplete;

    // This is the method to run when the timer is raised. 
    private void TimerEventProcessor(Object myObject,
                                            EventArgs myEventArgs)
    {
        timer.Stop();
        _onComplete();
    }

    public WaitTwoSeconds(Action onComplete)
    {
        _onComplete = onComplete;
        timer.Tick += new EventHandler(TimerEventProcessor);
        timer.Interval = new TimeSpan(0, 0, 2); // one second
        timer.Start();

    }
}

And in your code

private WaitTwoSeconds waitTimer;
private void SomeButtonHandlerOrSomething(
    object sender, ButtonClickedEventArgsLol e)
{    
    waitTimer = new WaitTwoSeconds(AfterTwoSeconds);
}

private void AfterTwoSeconds()
{
    // do whatever
}

This design isn't that good, but it should give you a clear understanding of how multithreading works. If you aren't doing something, then don't block.

Upvotes: 0

Igor Kulman
Igor Kulman

Reputation: 16369

This can be done much more easier using Reactive Extensions (reference Microsoft.Phone.Reactive):

Observable.Timer(TimeSpan.FromSeconds(2)).Subscribe(_=>{
    //code to be executed after two seconds
});

Beware that the code will not be executed on the UI thread so you may need to use the Dispatcher.

Upvotes: 2

Related Questions