3

I have an object that moves. This object is represented by a list of points. The calculation is done in a seperate thread. To draw this point there is a window that has a WriteableBitmap attached. The drawing method looks something like this.

  • Clear the WriteableBitmap
  • ForEach point draw it to the bitmap
  • Call the InvalidateVisual() method of the window trough the window dispatcher

If this is done frequently (every 100ms) the result is a window that flickers. How can i avoid that?

Edit: Sample Code

Code of the drawing component

public void FillColor(Color color)
    {
        writeableBitmap.Clear(Colors.White);
        writeableBitmap.Clear(color);
        window.Dispatcher.Invoke(
        DispatcherPriority.Render, new Action(() => window.InvalidateVisual()));

    }

Clear() set the whole Bitmap to a specific color.

The second thread invokes the drawing.

private void DrawObjectFull()
    {
        while (true)
        {
            window.FillColor(Colors.BurlyWood);
            Thread.Sleep(100);
        }

    }

Edit #2: I'm not entirely sure if this is the best approach. In the end i just want to draw a moving object from a to b smoothly.

nero.ch
  • 43
  • 3
  • What operating system are you using? http://stackoverflow.com/questions/1049446/wpf-how-do-i-prevent-tearing-with-writeablebitmap – Gusdor Nov 25 '13 at 08:56
  • I guess the InvalidateVisual call is not necessary. Could you perhaps post your relevant code so that people can comprehend what you are doing? – Clemens Nov 25 '13 at 10:13
  • I created a simple application with the same effect. I added that code to my question. – nero.ch Nov 25 '13 at 12:21

0 Answers0