Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. How to load an image, then wait a few seconds, then play a mp3 sound ?

How to load an image, then wait a few seconds, then play a mp3 sound ?

Scheduled Pinned Locked Moved Windows Forms
helptutorialquestion
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ibmkahm
    wrote on last edited by
    #1

    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

    G B R 3 Replies Last reply
    0
    • I ibmkahm

      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

      G Offline
      G Offline
      Geoff Williams
      wrote on last edited by
      #2

      After you've loaded your image, and before the call to Thread.Sleep() you need to call Application.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);

      I L 2 Replies Last reply
      0
      • G Geoff Williams

        After you've loaded your image, and before the call to Thread.Sleep() you need to call Application.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);

        I Offline
        I Offline
        ibmkahm
        wrote on last edited by
        #3

        thank you a lot for the answer !!!! it works now :-)

        1 Reply Last reply
        0
        • G Geoff Williams

          After you've loaded your image, and before the call to Thread.Sleep() you need to call Application.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);

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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]


          M 1 Reply Last reply
          0
          • L Luc Pattyn

            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]


            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            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

            L 1 Reply Last reply
            0
            • M Mycroft Holmes

              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

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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]


              1 Reply Last reply
              0
              • I ibmkahm

                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

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #7

                [^] 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

                L 1 Reply Last reply
                0
                • B BillWoodruff

                  [^] 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

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  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]


                  1 Reply Last reply
                  0
                  • I ibmkahm

                    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

                    R Offline
                    R Offline
                    rockracker
                    wrote on last edited by
                    #9

                    try this idea //load your picture //create a timer then set ur time //create a mp3 player //play your sounds thats it

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups