2013-03-07 46 views
2

这是我更新的代码的ListBox有时我的listBox1或值更新里面闪烁第二或更少如何解决它?

this.Invoke(new Action(() => data = new List<string>())); 
this.Invoke(new Action(() => data.Add("Gpu Temeprature --- " + sensor.Value.ToString()))); 
this.Invoke(new Action(() => listBox1.DataSource = null)); 
this.Invoke(new Action(() => listBox1.DataSource = data)); 
this.Invoke(new Action(() => listBox1.Invalidate())); 

可变数据是List<string>一次我增加了一个新的数据所以现在传感器(sensor.Value)的值上的地方i被更新意味着每次删除旧值并添加新值。

有时物品本身在ListBox闪烁不到一秒钟左右,有时只有sensor.Value闪烁。

试图添加一个Validate()ListBox没有帮助。

这是后台做的工作事件:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{ 
    BackgroundWorker worker = sender as BackgroundWorker; 
    while (true) 
    { 

     if ((worker.CancellationPending == true)) 
     { 
      e.Cancel = true; 
      break; 
     } 
     else 
     { 
      if (tempCpuValue >= (float?)nud1.Value || tempGpuValue >= (float?)nud1.Value) 
      { 
       soundPlay = true; 
       blinking_label(); 
       NudgeMe(); 
      } 
      else 
      { 
       soundPlay = false; 
       stop_alarm = true; 

      } 
      cpuView(); 
      gpuView(); 
     } 
    } 
} 

我有ListBox的两个事件:

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e) 
{ 
    e.ItemHeight = 25; 
} 

private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    if (e.Index == -1) 
    { 
    } 
    else 
    { 

     ColorText.ColorListBox(data, e); 

    } 
} 

而且从另一个类至极的颜色ColorText功能ListBox中的项目:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Drawing; 
using System.Windows.Forms; 

namespace GatherLinks 
{ 
    class ColorText 
    { 


     public static void Texts(RichTextBox box, string text, Color color) 
     { 
      box.SelectionStart = box.TextLength; 
      box.SelectionLength = 0; 

      box.SelectionColor = color; 
      box.AppendText(text); 
      box.SelectionColor = box.ForeColor; 
     } 

     public static void ColorListBox(List<string> data, DrawItemEventArgs e) 
     { 
      string strLeft = null; 
      string strMid = "---"; 
      string strRight = null; 
      if (data[e.Index].Contains(strMid)) 
      { 
       int index = data[e.Index].IndexOf(strMid); 
       strLeft = data[e.Index].Substring(0, index); 
       strRight = data[e.Index].Substring(index + strMid.Length); 
      } 

      using (Font f = new Font(FontFamily.GenericSansSerif, 20, FontStyle.Regular)) 
      { 
       float startPos; 
       e.Graphics.DrawString(strLeft, f, Brushes.Red, e.Bounds.X, e.Bounds.Y); 
       startPos = e.Graphics.MeasureString(strLeft, f).Width; 
       e.Graphics.DrawString(strMid, f, Brushes.Black, e.Bounds.X + startPos, e.Bounds.Y); 
       startPos = e.Graphics.MeasureString(strLeft + strMid, f).Width; 
       e.Graphics.DrawString(strRight, f, Brushes.Green, e.Bounds.X + startPos, e.Bounds.Y); 
      } 
     } 
    } 
} 

我怎样才能使ListBox所以它不会每隔几秒或每次闪烁一次新的传感器。值是否更新?我不知道为什么以及何时发生,但ListBox项目和sensor.Value闪烁。

回答

0

我认为问题来自您清除DataSource然后重新设置它的事实。不需要将数据源设置为null,然后在数据更改时将其重置。

尝试删除这两条线,看看是否闪烁消失:

this.Invoke(new Action(() => listBox1.DataSource = null)); 
this.Invoke(new Action(() => listBox1.DataSource = data)); 
0

Invoke方法的用法是错误的,可能是闪烁的原因。

试图改变你的代码的第一部分是这样的:

this.Invoke(new Action(() => 
    { 
     data = new List<string>(); 
     data.Add("Gpu Temeprature --- " + sensor.Value.ToString()); 
     listBox1.DataSource = null; 
     listBox1.DataSource = data; 
     listBox1.Invalidate() 
    })); 

每次调用Invoke方法,一个消息发布到应用程序的消息队列,后来在UI线程处理时UI不忙。通过多次呼叫Invoke,您将发送多条消息,这些消息将被单独处理。

0

一旦我解决了这个把我的形式:

protected override CreateParams CreateParams 
{ 
    get 
    { 
     CreateParams cp = base.CreateParams; 
     cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED 
     return cp; 
    } 
} 
相关问题