//更新红绿灯状态
public void UpdateLight(object obj)
{
try
{
Dictionary<string, string> dic_light = obj as Dictionary<string, string>;
List<Dictionary<string, string>> dic_lights = new List<Dictionary<string, string>>();
for (int i = 0; i < 4; i++)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (var item in dic_light)
{
if (Convert.ToInt32(item.Key) > i * 40 && Convert.ToInt32(item.Key) <= (i + 1) * 40)
{
dic.Add(item.Key, item.Value);
}
}
dic_lights.Add(dic);
}
for (int i = 0; i < dic_lights.Count; i++)
{
Thread thread = new Thread(new ParameterizedThreadStart(DealLight));
thread.Start(dic_lights[i]);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("--------更新红绿灯异常------" + ex.ToString());
return;
}
} //更新红绿灯状态
public void DealLight(object obj)
{
Dictionary<string, string> dic_light = obj as Dictionary<string, string>;
try
{
foreach (PictureBox pic in listLightPic)
{
foreach (var item in dic_light)
{
//pic.Name格式:light_11
if (pic.Name.Substring(pic.Name.IndexOf('_') + 1) == item.Key)
{
this.Invoke(new MyInvokeUpdateLight(SetLightPicImage), pic, item.Value);
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("--------更新红绿灯异常------" + ex.ToString());
return;
}
} //给红绿灯设置对应背景图片
public void SetLightPicImage(PictureBox pic, string value)
{
switch (value)
{
case "绿色":
pic.BackgroundImage = image_lv;
break;
case "红色":
pic.BackgroundImage = image_hong;
break;
case "绿闪":
pic.BackgroundImage = image_lvshan;
break;
case "红闪":
pic.BackgroundImage = image_hongshan;
break;
case "故障":
pic.BackgroundImage = image_guzhang;
break;
}
}我WINFORM程序界面上有有160个picturebox控件,每一个控件对应一个红绿灯。
现在的情况是我在笔记本上这样做,每个picturebox的背景图片基本能达到同时更新。但是我把这程序移植到一个硬件配置比较差的电脑上运行的时候,就出现是每个picturebox依次一个个的更新,不像在笔记本上那样能几乎同时更新。让我困惑的是,我是用几个线程分别去更新的(分四个线程更新,每个线程去更新40个picturebox的背景图片),为什么会出现所有的picturebox依次一个个去更新呢?真是电脑性能问题吗?劳烦大神帮忙看下啊多线程C# PictureBox
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货