开发者

Dispatcher events are rendered too fast on screen. How can I "slow down" the process?

开发者 https://www.devze.com 2023-02-17 16:11 出处:网络
I have a UniformGrid, with each cell having the property of being colored white or red. I want to have a loop which runs on all the grid\'s coordinates (top to bottom, left to right) and paint the gri

I have a UniformGrid, with each cell having the property of being colored white or red. I want to have a loop which runs on all the grid's coordinates (top to bottom, left to right) and paint the grid cells red, but I wish to do so that it looks like an animation - a red line filling the grid. In reality, the grid just fill up entirely in a second, thus no illusion of animation.

I have a loop running which calls the UI Dispatcher and sends it the "paintCell" function, w开发者_StackOverflow社区hich colors the cell (red or white). Supposedly, it seems as if the Dispatcher object runs the paintCell code but doesn't actually re-paint the screen until all the queued rendering events are worked on and removed from the queue. I tried adding: "Thread.sleep(500)", thinking I might slow down the whole coloring-and-rendering section, but the program acts the same concerning the interval between cell colorings - the program does sleep, but the whole grid is till repainted and rendered at once.

Any clarifications needed?


You shouldn't Sleep in the UI thread (which I suspect you do), as this blocks the whole UI for the duration of Sleep. You should do the sleep in your loop, which sends the updates through the Dispatcher (you run the loop in a separate thread, of course, right?)


You should use a DispatcherTimer for your painting operations. The DispatcherTimer runs on the UI thread, so you can call your paintCell method in there without any extra work:

   DispatcherTimer _timer = new DispatcherTimer();
   void StartTimer()
   {            
        _timer.Interval = new TimeSpan(0, 0, timerResolutionInSeconds);
        _timer.Tick += (s, e) => TimerElapsed();
        _timer.Start();
   }

   int cell;
   private void TimerElapsed()
   {
        paintCell(cell++);
        // .... stop timer when done
        if (done)
           _timer.Stop()
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消