Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用c暂停图像#_C#_Image_Timer_Slideshow - Fatal编程技术网

C# 用c暂停图像#

C# 用c暂停图像#,c#,image,timer,slideshow,C#,Image,Timer,Slideshow,我想制作一个程序,每5秒钟显示一幅图像,然后暂停1秒钟,然后再显示下一幅。但我在暂停图像时遇到问题。如果我有消息框代码,它会停止显示图像,我必须按“确定”继续,但如果没有显示图像,它会跳到最后一幅图像。请帮忙,我必须在代码中添加什么才能正常工作 private void button1_Click(object sender, EventArgs e) { string[] arr1 = new string[] { "water", "eat", "bath", "tv

我想制作一个程序,每5秒钟显示一幅图像,然后暂停1秒钟,然后再显示下一幅。但我在暂停图像时遇到问题。如果我有消息框代码,它会停止显示图像,我必须按“确定”继续,但如果没有显示图像,它会跳到最后一幅图像。请帮忙,我必须在代码中添加什么才能正常工作

private void button1_Click(object sender, EventArgs e)
{
    string[] arr1 =
        new string[] { "water", "eat", "bath", "tv", "park", "sleep" };

    for (int i = 0; i < 6; i++)
    {
        button1.BackgroundImage = 
            (Image)Properties.Resources.ResourceManager.GetObject(arr1[i]);
        new SoundPlayer(Properties.Resources.nero).Play();
        MessageBox.Show(arr1[i].ToString());

        Thread.Sleep(5000);
    }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
字符串[]arr1=
新字符串[]{“水”、“吃”、“洗澡”、“电视”、“公园”、“睡眠”};
对于(int i=0;i<6;i++)
{
按钮1.背景图片=
(图像)Properties.Resources.ResourceManager.GetObject(arr1[i]);
新的SoundPlayer(Properties.Resources.nero.Play();
Show(arr1[i].ToString());
睡眠(5000);
}
}

问题在于UI线程没有机会更新显示。也就是说:图像正在显示,但UI不会更新

一个不太好的方法是使用
Application.DoEvents
让UI像这样自我更新:

for (int i = 0; i < 6; i++)
{
    button1.BackgroundImage = (Image)Properties.Resources.ResourceManager.GetObject(arr1[i]);
    Application.DoEvents();

    new SoundPlayer(Properties.Resources.nero).Play();

    Thread.Sleep(5000);
}
在计时器事件中,使用以下代码:

private void timer_Tick(object sender, EventArgs e)
{    
    timer.Stop();
    try
    {
        timer.Interval = 5000; // Next time, wait 5 secs

        // Set the image and select next picture
        button1.BackgroundImage = (Image)Properties.Resources.ResourceManager.GetObject(arr1[currentImageIndex]);
        currentImageIndex++;
    }
    finally
    {
        // Only start the timer if we have more images to show!
        if (currentImageIndex < arr1.Length)   
            timer.Start();
    }
}
private void timer_Tick(对象发送方,事件参数e)
{    
timer.Stop();
尝试
{
timer.Interval=5000;//下次,请等待5秒
//设置图像并选择下一张图片
button1.BackgroundImage=(Image)Properties.Resources.ResourceManager.GetObject(arr1[currentImageIndex]);
currentImageIndex++;
}
最后
{
//只有当我们有更多图像要显示时才启动计时器!
if(currentImageIndex
最好的方法是与计时器控件结合使用。将计时器的间隔设置为1秒,但最初将其禁用。然后,按钮按下代码仅启用计时器

public partial class MyForm : Form
{
    private int ImagePosition = 0;
    private string[] arr1 = new string[] { "water", "eat", "bath", "tv", "park", "sleep" };

    private void button1_Click(object sender, EventArgs e)
    {
        ImagePosition = 0;
        RotateImage();
        Timer1.Start();      
    }

    private void RotateImage()
    {
        button1.BackgroundImage = 
        (Image)Properties.Resources.ResourceManager.GetObject(arr1[ImagePosition]);
        new SoundPlayer(Properties.Resources.nero).Play();
        ImagePosition++;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (ImagePosition > 5)
        {
            timer1.Enabled = false;
            return;
        }

        RotateImage();
    }
}

我很困惑消息框在这里的作用,但更进一步地说,
Thread.Sleep
不是正确的解决方案。消息框可能只是一个调试解决方案,用于查看循环是否运行。当然,在关闭该框后,UI会刷新,没有消息框就不会刷新。消息框不是我需要的东西,它只是为了测试它是否可以工作,在这里可能还可以,但是DoEvents()不可以可能会有一些危险的副作用。按下按钮时使用计时器的正确代码是什么?@marios正在编辑答案,而你在写评论。请看代码。@JoelCoehoorn我读到不建议使用
DoEvents
,但我从来都不明白为什么-你是快速阅读负面影响还是我需要谷歌搜索?:-)此代码的负面影响:计时器将等待一秒钟,然后显示第一幅图像。另外,我还想指出,基于语法约定,变量名以小写字母开头(C#:-)修复了延迟问题:-d这是一个很好的解决方案。
public partial class MyForm : Form
{
    private int ImagePosition = 0;
    private string[] arr1 = new string[] { "water", "eat", "bath", "tv", "park", "sleep" };

    private void button1_Click(object sender, EventArgs e)
    {
        ImagePosition = 0;
        RotateImage();
        Timer1.Start();      
    }

    private void RotateImage()
    {
        button1.BackgroundImage = 
        (Image)Properties.Resources.ResourceManager.GetObject(arr1[ImagePosition]);
        new SoundPlayer(Properties.Resources.nero).Play();
        ImagePosition++;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (ImagePosition > 5)
        {
            timer1.Enabled = false;
            return;
        }

        RotateImage();
    }
}