0

I have a Table control, and I want to fill it with colors, with a delay of X milliseconds between each paint.

This a very general example of what I am doing (It's longer, but I deleted the unimportant lines):

private void myFunc()
{
     while (SOME_CONDITION) {
          Thread.Sleep(X_MS);
          this.myTable.RowGroups[0].Rows[SOME_ROW].Cells[SOME_COLUMN].Background = Brushes.Blue;
     }
}

The above just freezes the Window until the function is done, and paints the desired cells in a single moment.

I know I shouldn't use Sleep, but when I worked on a console application, it was an easy solution. Any suggestions?

Novak
  • 2,760
  • 9
  • 42
  • 63
  • 1
    your are blocking the current UI thread by doing a sleep. answer articulated/can be derived from http://stackoverflow.com/questions/8078596/sleep-inside-for-loop-in-wpf – Krishna Jun 10 '13 at 09:08
  • You may use a DispatcherTimer as shown in [this answer](http://stackoverflow.com/a/16978840/1136211). – Clemens Jun 10 '13 at 09:10

1 Answers1

2

you can use new async features

private async void myFunc()
{
    while (SOME_CONDITION) {
        await Task.Delay(X_MS);
        this.myTable.RowGroups[0].Rows[SOME_ROW].Cells[SOME_COLUMN].Background = Brushes.Blue;
    }
}

because of WPF`s synchronization context table editig will be performed on UI thread

Oleh Nechytailo
  • 2,155
  • 17
  • 26