У меня есть приведенный ниже код, который отлично работает в других приложениях.
В моем приложении у меня есть 4 потока, вызывающие метод AddToList каждые 60 мс.
Как только он получил 1000 элементов в спискеи начал пытаться удалить предметы, процессор будет идти на 100%.Если уменьшить счет до 100, это будет исправлено.
Есть идеи почему?
Вот код:
public delegate void dgAddToList(string Message, int InputID);
public void AddToList(string Message, int InputID)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new dgAddToList(AddToList), new object[] { Message, InputID });
}
else
{
switch (InputID)
{
case 0:
this.listBox1.Items.Insert(0, Message);
if (this.listBox1.Items.Count > 100)
this.listBox1.Items.RemoveAt(this.listBox1.Items.Count - 1);
break;
case 1:
this.listBox2.Items.Insert(0, Message);
if (this.listBox2.Items.Count > 100)
this.listBox2.Items.RemoveAt(this.listBox2.Items.Count - 1);
break;
case 2:
this.listBox3.Items.Insert(0, Message);
if (this.listBox3.Items.Count > 100)
this.listBox3.Items.RemoveAt(this.listBox3.Items.Count - 1);
break;
case 3:
this.listBox4.Items.Insert(0, Message);
if (this.listBox4.Items.Count > 100)
this.listBox4.Items.RemoveAt(this.listBox4.Items.Count - 1);
break;
}
}
}
ОБНОВЛЕНИЕ: Просто чтобы уточнить.Первый поток будет обновлять только Listbox1, второй поток будет обновлять Listbox 2. Это определяется параметром InputID, поэтому Thread1 передает 0, а Thread 2 передает 1