如何加载图像,然后等待几秒钟,然后播放 mp3 声音?

发布于 2024-08-19 03:13:22 字数 606 浏览 6 评论 0原文

按下按钮后,我想显示图像(使用图片框),等待几秒钟,然后播放 mp3 声音,但我无法使用。为了等待几秒钟,我使用了 System.Threading.Thread.Sleep(5000)。问题是,图像总是在等待时间之后出现,但我希望它先显示,然后等待,然后播放 mp3...我尝试使用 WaitOnLoad = true 但它不起作用,难道不应该先加载图像然后继续读取下一个代码行吗?

这是我尝试过的代码(不起作用):

private void button1_Click(object sender, EventArgs e) {
    pictureBox1.WaitOnLoad = true;
    pictureBox1.Load("image.jpg");
    System.Threading.Thread.Sleep(5000);
    MessageBox.Show("test");//just to test, here should be the code to play the mp3
}

我还尝试使用“LoadAsync”加载图像,并将代码放置在“LoadCompleted”事件中等待并播放 mp3,但这也不起作用...

After pressing a button, I would like to show an image (using a picturebox), wait a few seconds and then play a
mp3 sound, but i dont get it to work. To wait a few seconds I use System.Threading.Thread.Sleep(5000). The problem is, the image always appears after the wait time, but I want it to show first, then wait, then play the mp3... I tried to use WaitOnLoad = true but it doesn't work, shouldn't it load the image first and the continue to read the next code line?

Here is the code I've tried (that doesn't work):

private void button1_Click(object sender, EventArgs e) {
    pictureBox1.WaitOnLoad = true;
    pictureBox1.Load("image.jpg");
    System.Threading.Thread.Sleep(5000);
    MessageBox.Show("test");//just to test, here should be the code to play the mp3
}

I also tried loading the image with "LoadAsync" and put the code to wait and play the mp3 in the "LoadCompleted" event, but that doesn't work either...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白况 2024-08-26 03:13:22

我将使用 LoadCompleted 事件并在图像加载后以 5 秒的间隔启动计时器,以便 UI 线程不会被阻塞:

   private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.WaitOnLoad = false;
        pictureBox1.LoadCompleted += new AsyncCompletedEventHandler(pictureBox1_LoadCompleted);
        pictureBox1.LoadAsync("image.jpg");
    }

    void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
    {
        //System.Timers.Timer is used as it supports multithreaded invocations
        System.Timers.Timer timer = new System.Timers.Timer(5000); 

        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

        //set this so that the timer is stopped once the elaplsed event is fired
        timer.AutoReset = false; 

        timer.Enabled = true;
    }

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        MessageBox.Show("test"); //just to test, here should be the code to play the mp3
    }

I would use the LoadCompleted event and start a timer with 5 sec interval once the image is loaded, so that the UI thread is not blocked:

   private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.WaitOnLoad = false;
        pictureBox1.LoadCompleted += new AsyncCompletedEventHandler(pictureBox1_LoadCompleted);
        pictureBox1.LoadAsync("image.jpg");
    }

    void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
    {
        //System.Timers.Timer is used as it supports multithreaded invocations
        System.Timers.Timer timer = new System.Timers.Timer(5000); 

        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

        //set this so that the timer is stopped once the elaplsed event is fired
        timer.AutoReset = false; 

        timer.Enabled = true;
    }

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        MessageBox.Show("test"); //just to test, here should be the code to play the mp3
    }
阳光下慵懒的猫 2024-08-26 03:13:22

您是否在等待时间之前尝试过使用 Application.DoEvents(); ?我相信这应该强制 C# 在睡觉之前绘制图像。

Have you tried using Application.DoEvents(); before the wait time? I believe that should force C# to draw the image before going to sleep.

才能让你更想念 2024-08-26 03:13:22

它在使用 application.doevents() 时起作用。

private void button1_Click(object sender, EventArgs e) 
{
    pictureBox1.Load("image.jpg");
    Application.DoEvents();
    pictureBox1.WaitOnLoad = true;
    System.Threading.Thread.Sleep(5000);
    MessageBox.Show("test"); //just to test, here should be the code to play the mp3
}

It works when application.doevents() is used.

private void button1_Click(object sender, EventArgs e) 
{
    pictureBox1.Load("image.jpg");
    Application.DoEvents();
    pictureBox1.WaitOnLoad = true;
    System.Threading.Thread.Sleep(5000);
    MessageBox.Show("test"); //just to test, here should be the code to play the mp3
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文