如何加载图像,然后等待几秒钟,然后播放 mp3 声音?
按下按钮后,我想显示图像(使用图片框),等待几秒钟,然后播放 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将使用 LoadCompleted 事件并在图像加载后以 5 秒的间隔启动计时器,以便 UI 线程不会被阻塞:
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:
您是否在等待时间之前尝试过使用
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.它在使用
application.doevents()
时起作用。It works when
application.doevents()
is used.