Reputation: 503
I am updating a "timer" field in my listView from different threads. It works fine, the problem is just that it flickers. This is the code each thread is calling when it needs to be updated (almost every second).
private void AddToListView(string user, string status, string proxy, int number)
{
Invoke(new MethodInvoker(
delegate
{
listView1.BeginUpdate();
this.listView1.Items[number].SubItems[1].Text = status;
listView1.EndUpdate();
}
));
}
Having Googled a bit im not even sure i can make this flicker go away? :/
Upvotes: 2
Views: 1566
Reputation: 48969
I would not use Invoke
here. In fact, in most cases it is usually not a great option despite what you may read on the internet. Instead, package up the data being generated by the thread into a POCO and put it into a queue. Have a System.Windows.Forms.Timer
tick every second with the event handler pulling items out of the queue to update the ListView
in batches. Also, try setting DoubleBuffered
to true. These suggestions should help some.
public class YourForm : Form
{
private ConcurrentQueue<UpdateInfo> queue = new ConcurrentQueue<UpdateInfo>();
private void YourTimer_Tick(object sender, EventArgs args)
{
UpdateInfo value;
listView1.BeginUpdate();
while (queue.TryDequeue(out value)
{
this.listView1.Items[value.Number].SubItems[1].Text = value.Status;
}
listView1.EndUpdate();
}
private void SomeThread()
{
while (true)
{
UpdateInfo value = GetUpdateInfo();
queue.Enqueue(value);
}
}
private class UpdateInfo
{
public string User { get; set; }
public string Status { get; set; }
public string Proxy { get; set; }
public int Number { get; set; }
}
}
Upvotes: 2