我有一个程序在工作时用不同的图标显示它的进度。该过程通过按下 btnApply 按钮开始。
ManualResetEvent[] wait;
private void btnApply_Click(object sender, EventArgs e)
{
wait = new ManualResetEvent[1];
wait[0] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(doApply), 0);
WaitHandle.WaitAny(wait);
btnApply.Text = "Done!";
}
private void doApply(object dummy)
{
for (int i = 0; i < 14; i++)
{
setPic(i, bmTODO);
}
// do something
for (int i = 0; i < 14; i++)
{
setPic(i, bmWORK);
// do something
setPic(i, bmOK);
}
}
public void setPic(int i, Image img)
{
switch (i)
{
case 0: pictureBox1.Image = img; break;
case 1: pictureBox2.Image = img; break;
case 2: pictureBox3.Image = img; break;
case 3: pictureBox4.Image = img; break;
case 4: pictureBox5.Image = img; break;
case 5: pictureBox6.Image = img; break;
case 6: pictureBox7.Image = img; break;
case 7: pictureBox8.Image = img; break;
case 8: pictureBox9.Image = img; break;
case 9: pictureBox10.Image = img; break;
case 10: pictureBox11.Image = img; break;
case 11: pictureBox12.Image = img; break;
case 12: pictureBox13.Image = img; break;
case 13: pictureBox14.Image = img; break;
default: break;
}
}
使用 Win7 和 2 处理器,这可以“按应有的方式”工作。
但在 XP 上,我必须切换任务 - 但我怎样才能在这个特殊的代码中做到这一点?我不想在 doApply 中指定每个图片框。
我也尝试过委托:
public delegate void setPicDelegate(int i, Image img);
但是没有令人满意的结果:程序挂起或图标最近一起显示在最后。
我能做些什么?