C#秒表问题

时间:2015-01-09 00:17:50

标签: c# stopwatch

private void button3_Click(object sender, EventArgs e)
{  
    var stopwatch = Stopwatch.StartNew();
    for (int index = 1; index < timeNum; index++)
    {
        stopwatch.Restart(); 
        //MessageBox.Show(“test”);
        Thread.Sleep(5000);  
        pictureBox1.Image = list1[index * 2];
        pictureBox2.Image = list1[index * 2 + 1];
        stopwatch.Stop();
    }
 }

我希望每隔一段时间更改两个图片框的图像,但程序不会自动执行,任何人都可以提供帮助,谢谢

2 个答案:

答案 0 :(得分:0)

在评论中,您不需要秒表。 情况可能是timeNum <= 1。

private void button3_Click(object sender, EventArgs e)
{  
    //Default the pictures first
    pictureBox1.Image = list1[0];//Verify that this exists as well
    pictureBox2.Image = list1[1];//Verify that this exists as well

    if(timeNum <= 1){
        MessageBox.Show(“oHs Noes!!!!!!”);
    }
    else{
        for (int index = 1; index < timeNum; index++)
        {
            Thread.Sleep(5000);  
            pictureBox1.Image = list1[index * 2]; //You will need to verify that this exists as well
            pictureBox2.Image = list1[index * 2 + 1];//You will need to verify that this exists as well
        }
    }
}

&#34;程序不会自动执行,&#34; - &GT;这是什么意思? 我假设你想让它发挥作用。逻辑就在那里。

答案 1 :(得分:0)

如果您的意思是自动刷新图片框。在这里。

private void button3_Click(object sender, EventArgs e)
{  
    var stopwatch = Stopwatch.StartNew();
    for (int index = 1; index < timeNum; index++)
    {
        //stopwatch.Restart(); // as they say, you don't need this
        //MessageBox.Show(“test”);
        Thread.Sleep(5000);  
        pictureBox1.Image = list1[index * 2];
        pictureBox2.Image = list1[index * 2 + 1];
        //stopwatch.Stop();
        pictureBox1.Update();
        pictureBox2.Update();
    }
}