Вопрос

I'm starting two back-to-back processes in WPF with a UI change in between. There is also a while loop that wraps it all. So, the code looks similar to this:

while(stackpanel.Children.Count != 0)
{
   Grid grid = stackpanel.Children[0] as Grid;

   // start process 1 and WaitForExit()

   stackpanel.Remove(grid);

   // start process 2 and WaitForExit()
}

The problem is that the UI isn't updating after the first process exists and before the second one starts. It only updates once when the while loop terminates.

How could I implement the above logic and be able to redraw stackpanel?

Это было полезно?

Решение

You are waiting on UI thread which won't let refresh your UI until you exit from method. I strongly suggest to move your long running process on to seperate thread and wait on it and once done, marshall you call back to UI thread via Dispatcher.

However, you can use workaround by simply queuing an empty delegate on UI dispatcher with priority set to render so that your UI gets refresh -

   while(stackpanel.Children.Count != 0)
   {
      Grid grid = stackpanel.Children[0] as Grid;

      // start process 1 and WaitForExit()

      stackpanel.Remove(grid);

      Dispatcher.Invoke((Action)(() => { }), DispatcherPriority.Render);

      // start process 2 and WaitForExit()
    }

Refer to my answer over here for details.

Другие советы

Did you try these: UpdateLayout() ? stackPanel.InvalidateVisual() ? maybe a PropertyChanged with String.Empty ?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top