0

Visual Studio 2010を使用して、Win Phoneの画像処理を行っています。画像を2秒間表示するために(スライドショーのように)、次のクラスが呼び出されます。

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

これは次のように呼び出されます。

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

はエラーとして要求されているため、Application.DoEvents();「System.Windows.Application」には「DoEvents」の定義が含まれていません。だから私はそのコードブロックを削除しました

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

プログラムをコンパイルして実行すると、履歴書が表示されます...
これを修正するにはどうすればよいですか?ありがとう

4

2 に答える 2

2

これは、Reactive Extensions (Microsoft.Phone.Reactive を参照) を使用すると、はるかに簡単に実行できます。

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

コードは UI スレッドでは実行されないため、Dispatcher を使用する必要がある場合があることに注意してください。

于 2013-02-07T21:48:09.210 に答える
0

マルチスレッドを採用します。

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

    }
}

そしてあなたのコードで

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

private void AfterTwoSeconds()
{
    // do whatever
}

この設計はあまり良くありませんが、マルチスレッドがどのように機能するかを明確に理解できるはずです。もしていない場合は、ブロックしないでください。

于 2013-02-07T21:55:48.190 に答える