views:

327

answers:

2

Hello there,

My application is multithreaded it has f.ex 25 active threads, and each thread pushes it status to list view item by delegate.

example:

    private delegate void SetBackColorDelegate(int index, Color color);
    private void SetBackColor(int index, Color color)
    {
        if (listView1.InvokeRequired)
        {
              listView1.Invoke(new SetBackColorDelegate(SetBackColor), new object[] { index, color });

        }
        else
        {
              listView1.Items[index].BackColor = color;
        }
    }

Depending on status it changes item color and etc. And it twinkles a lot, it looks very nasty :)

Maybe you can suggest how to avoid this ? How to speedup drawing ? Or maybe I should consider of start using some different component ?

+1  A: 

Consider making each thread push its state to some shared data structure, and then poll that once per second from the UI thread using a timer. That way:

  • You don't have to worry about invoking from the non-UI threads
  • You won't get a "busy" UI
  • You'll still be reasonably up-to-date (vs an approach of saying "don't update if I've already updated recently)

I don't know much about UI timers in terms of efficiency - it's possible that such a timer would be relatively costly, so you may need to have some cross-thread communication anyway: make each non-UI thread enable the timer if it's not already enabled, and disable it when you do the update. That way you'll always be a second behind reality, but still with only one status update per second.

Jon Skeet
Yeah, It can be done easily. But I need instant status ;) that's the problem, I could do that each one sec, but I can't.
Lukas Šalkauskas
Then it sounds like you've got contradictory requirements: if you need to update something instantly, and you've got a lot of updates to display, then the screen will show all those updates, giving a flickering effect.
Jon Skeet
(If this is really just double buffering, then I misunderstood the problem - oops.)
Jon Skeet
+2  A: 

While I wait for your response to my comment. If the flickering is a double buffering issue then the accepted answer from this question will get you on your way. Set your listview to use the style from the answer and you're good to go. There is a performance penalty for this since it will ensure that the colour updates only happen in sync with the monitor's refresh rate (normally around 60 times per second), but it will stop the flicking/tearing that occurs when the updates fall between monitor refreshes.

Martin Harris
Thanks :) actualy this http://stackoverflow.com/questions/76993/how-to-double-buffer-net-controls-on-a-form helped me :)
Lukas Šalkauskas