How to load an image, then wait a few seconds, then play a mp3 sound ?
-
Hello everybody, (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 alway 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 doesnt 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 doesnt 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 doesnt work either...) would be very nice if somebody helps me
-
Hello everybody, (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 alway 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 doesnt 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 doesnt 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 doesnt work either...) would be very nice if somebody helps me
After you've loaded your image, and before the call to
Thread.Sleep()
you need to callApplication.DoEvents()
to enable your form to be updated with the image.pictureBox1.Load("image.jpg");
System.Windows.Forms.Application.DoEvents(); // this allows the form to be updated
System.Threading.Thread.Sleep(5000); -
After you've loaded your image, and before the call to
Thread.Sleep()
you need to callApplication.DoEvents()
to enable your form to be updated with the image.pictureBox1.Load("image.jpg");
System.Windows.Forms.Application.DoEvents(); // this allows the form to be updated
System.Threading.Thread.Sleep(5000); -
After you've loaded your image, and before the call to
Thread.Sleep()
you need to callApplication.DoEvents()
to enable your form to be updated with the image.pictureBox1.Load("image.jpg");
System.Windows.Forms.Application.DoEvents(); // this allows the form to be updated
System.Threading.Thread.Sleep(5000);calling DoEvents() like that is a hack one should avoid. There are two correct ways to solve such problems: 1. use a timer, preferably a System.Windows.Forms.Timer if the delayed action relates to the GUI; 2. use a separate thread (e.g. a BackgroundWorker), this time with proper Control.InvokeRequired/Imvoke (see here[^]). Both take a little more effort, but will not fail you, as DoEvents() would when your app becomes somewhat more complex. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
calling DoEvents() like that is a hack one should avoid. There are two correct ways to solve such problems: 1. use a timer, preferably a System.Windows.Forms.Timer if the delayed action relates to the GUI; 2. use a separate thread (e.g. a BackgroundWorker), this time with proper Control.InvokeRequired/Imvoke (see here[^]). Both take a little more effort, but will not fail you, as DoEvents() would when your app becomes somewhat more complex. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
Luc Pattyn wrote:
as DoEvents() would when your app becomes somewhat more complex.
I'm not disputing your statement but do you have anything to back this (your threading articles live in some of my apps), link, white paper etc.
Never underestimate the power of human stupidity RAH
-
Luc Pattyn wrote:
as DoEvents() would when your app becomes somewhat more complex.
I'm not disputing your statement but do you have anything to back this (your threading articles live in some of my apps), link, white paper etc.
Never underestimate the power of human stupidity RAH
No, I don't have any backup, except for logic reasoning: DoEvents() runs another message loop, which means the message queue becomes active while probably executing an event handler already, potentially and unexpectedly turning its caller in a re-entered method, which risks all kinds of things, e.g. to upset your object's state, and maybe even to overflow the stack (imagine DoEvents inside a MouseMove handler). I would avoid DoEvents() as much as possible, as it is very dangerous; and I would typically not need to call it, as I tend to use threads for most everything except actions that will always finish in a few milliseconds. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
Hello everybody, (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 alway 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 doesnt 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 doesnt 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 doesnt work either...) would be very nice if somebody helps me
[^] Guess it doesn't hurt to ask everywhere possible. best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
-
[^] Guess it doesn't hurt to ask everywhere possible. best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
BillWoodruff wrote:
ask everywhere possible
That is the best way to get more wrong answers... the first one is correct and rather creative, not the way I would recommend though. The rest just stinks. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
Hello everybody, (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 alway 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 doesnt 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 doesnt 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 doesnt work either...) would be very nice if somebody helps me
try this idea //load your picture //create a timer then set ur time //create a mp3 player //play your sounds thats it