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. C#
  4. Is the command to check if the bell is running or stopped ?

Is the command to check if the bell is running or stopped ?

Scheduled Pinned Locked Moved C#
question
11 Posts 2 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.
  • U Offline
    U Offline
    User 2456424
    wrote on last edited by
    #1

    I have the following C # statement:

    ...
    using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
    {
    player.Play();

    //What should I write here?
    while (If the bell is running, wait here)
    {
    if (player.Stop()==true)
    {
    this.progressBarControl1.Visible = false; //false = không cho phép hiện
    }
    }
    }
    ...

    I want to check if the ringer is running or stopping, if it is running then wait until it stops before running this.progressBarControl1.Visible = false.

    Richard DeemingR 1 Reply Last reply
    0
    • U User 2456424

      I have the following C # statement:

      ...
      using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
      {
      player.Play();

      //What should I write here?
      while (If the bell is running, wait here)
      {
      if (player.Stop()==true)
      {
      this.progressBarControl1.Visible = false; //false = không cho phép hiện
      }
      }
      }
      ...

      I want to check if the ringer is running or stopping, if it is running then wait until it stops before running this.progressBarControl1.Visible = false.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      If you need to know when the sound has finished playing, you'll have to use the PlaySync method[^]. However, if you do that from the UI thread, your application will stop responding until the sound finishes. You'll probably want to play the sound on a background thread to avoid this problem. The simplest option would probably be to use a BackgroundWorker instance[^] - put the SoundPlayer code in the DoWork event handler, and the code to hide the progress bar in the RunWorkerCompleted event handler.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      U 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        If you need to know when the sound has finished playing, you'll have to use the PlaySync method[^]. However, if you do that from the UI thread, your application will stop responding until the sound finishes. You'll probably want to play the sound on a background thread to avoid this problem. The simplest option would probably be to use a BackgroundWorker instance[^] - put the SoundPlayer code in the DoWork event handler, and the code to hide the progress bar in the RunWorkerCompleted event handler.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        U Offline
        U Offline
        User 2456424
        wrote on last edited by
        #3

        use PlaySync() runs fine, but using backgroundWorker1 is difficult to use and the computer freezes

        Richard DeemingR 1 Reply Last reply
        0
        • U User 2456424

          use PlaySync() runs fine, but using backgroundWorker1 is difficult to use and the computer freezes

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Member 2458467 wrote:

          backgroundWorker1 is difficult to use and the computer freezes

          Then you're using it wrong.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          U 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Member 2458467 wrote:

            backgroundWorker1 is difficult to use and the computer freezes

            Then you're using it wrong.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            U Offline
            U Offline
            User 2456424
            wrote on last edited by
            #5

            I do not know how to use backgroundWorker in my code so the device crashes, I troubleshoot I see Debug.Print("Status: PLAY") and Debug.Print("Status: STOP") appear only once, You see my code, did I write anything wrong ?

            bool _isStopped = true;
            public Form1()
            {
            InitializeComponent();
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;

               lblStatusPlaySound.ForeColor = Color.Violet;
               lblStatusPlaySound.Text = "Status: PLAY/STOP";
            

            }
            private void btnPlaySound_Click(object sender, EventArgs e)
            {
            // Start the asynchronous operation.
            backgroundWorker1.RunWorkerAsync();

              using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
              {
                  player.Play();
              }
                    
              while (\_isStopped == true)
              {      
                 if (backgroundWorker1.IsBusy != true)
                 { \_isStopped = true; }
                 else //
                 { \_isStopped = false; }
            
                 lblStatusPlaySound.ForeColor = Color.Green;
                 lblStatusPlaySound.Text = "Status: PLAY";
                 lblStatusPlaySound.Refresh(); //                
                 Debug.Print("Status: PLAY");
             }
            
             lblStatusPlaySound.ForeColor = Color.Red;
             lblStatusPlaySound.Text = "Status: STOP";
             lblStatusPlaySound.Refresh();             
             Debug.Print("Status: STOP");
            

            }

            Richard DeemingR 1 Reply Last reply
            0
            • U User 2456424

              I do not know how to use backgroundWorker in my code so the device crashes, I troubleshoot I see Debug.Print("Status: PLAY") and Debug.Print("Status: STOP") appear only once, You see my code, did I write anything wrong ?

              bool _isStopped = true;
              public Form1()
              {
              InitializeComponent();
              backgroundWorker1.WorkerReportsProgress = true;
              backgroundWorker1.WorkerSupportsCancellation = true;

                 lblStatusPlaySound.ForeColor = Color.Violet;
                 lblStatusPlaySound.Text = "Status: PLAY/STOP";
              

              }
              private void btnPlaySound_Click(object sender, EventArgs e)
              {
              // Start the asynchronous operation.
              backgroundWorker1.RunWorkerAsync();

                using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
                {
                    player.Play();
                }
                      
                while (\_isStopped == true)
                {      
                   if (backgroundWorker1.IsBusy != true)
                   { \_isStopped = true; }
                   else //
                   { \_isStopped = false; }
              
                   lblStatusPlaySound.ForeColor = Color.Green;
                   lblStatusPlaySound.Text = "Status: PLAY";
                   lblStatusPlaySound.Refresh(); //                
                   Debug.Print("Status: PLAY");
               }
              
               lblStatusPlaySound.ForeColor = Color.Red;
               lblStatusPlaySound.Text = "Status: STOP";
               lblStatusPlaySound.Refresh();             
               Debug.Print("Status: STOP");
              

              }

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Read my previous message again: put the SoundPlayer code in the DoWork event handler, and the code to hide the progress bar in the RunWorkerCompleted event handler. You've put all of the code in the button's Click event handler instead. And as I said, you'll need to use the PlaySync method if you want to wait for the sound to finish playing. It should look something like:

              public Form1()
              {
              InitializeComponent();
              backgroundWorker1.DoWork += backgroundWorker1_DoWork;
              backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;

              lblStatusPlaySound.ForeColor = Color.Violet;
              lblStatusPlaySound.Text = "Status: PLAY/STOP";
              }

              private void btnPlaySound_Click(object sender, EventArgs e)
              {
              btnPlaySound.Enabled = false;
              lblStatusPlaySound.ForeColor = Color.Green;
              lblStatusPlaySound.Text = "Status: PLAY";
              backgroundWorker1.RunWorkerAsync();
              }

              private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
              {
              Debug.Print("Status: PLAY");

              using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
              {
                  player.PlaySync();
              }
              

              }

              private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
              {
              lblStatusPlaySound.ForeColor = Color.Red;
              lblStatusPlaySound.Text = "Status: STOP";
              lblStatusPlaySound.Refresh();
              btnPlaySound.Enabled = true;
              Debug.Print("Status: STOP");
              }


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              U 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Read my previous message again: put the SoundPlayer code in the DoWork event handler, and the code to hide the progress bar in the RunWorkerCompleted event handler. You've put all of the code in the button's Click event handler instead. And as I said, you'll need to use the PlaySync method if you want to wait for the sound to finish playing. It should look something like:

                public Form1()
                {
                InitializeComponent();
                backgroundWorker1.DoWork += backgroundWorker1_DoWork;
                backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;

                lblStatusPlaySound.ForeColor = Color.Violet;
                lblStatusPlaySound.Text = "Status: PLAY/STOP";
                }

                private void btnPlaySound_Click(object sender, EventArgs e)
                {
                btnPlaySound.Enabled = false;
                lblStatusPlaySound.ForeColor = Color.Green;
                lblStatusPlaySound.Text = "Status: PLAY";
                backgroundWorker1.RunWorkerAsync();
                }

                private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
                {
                Debug.Print("Status: PLAY");

                using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
                {
                    player.PlaySync();
                }
                

                }

                private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
                {
                lblStatusPlaySound.ForeColor = Color.Red;
                lblStatusPlaySound.Text = "Status: STOP";
                lblStatusPlaySound.Refresh();
                btnPlaySound.Enabled = true;
                Debug.Print("Status: STOP");
                }


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                U Offline
                U Offline
                User 2456424
                wrote on last edited by
                #7

                Hi Richard MacCutchan! Follow me, the above code can be edited without using RunWorkerCompleted, the program still returns the same result because the PlaySync() method is a sequential command, simple the code will be easier to understand, you see the code below. RunWorkerCompleted event in other cases eg Play() instead PlaySync() method to catch "PLAY/STOP" status for example, do you think ? that is my opinion.

                private void btnPlaySound_Click(object sender, EventArgs e)
                {
                lblStatusPlaySound.ForeColor = Color.Green;
                lblStatusPlaySound.Text = "Status: PLAY";
                Application.DoEvents();
                Debug.Print("Status: PLAY");

                  using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
                  {
                      player.PlaySync();
                  }
                
                  lblStatusPlaySound.ForeColor = Color.Red;
                  lblStatusPlaySound.Text = "Status: STOP";            
                  Application.DoEvents();         
                  Debug.Print("Status: STOP");
                

                }

                Richard DeemingR 1 Reply Last reply
                0
                • U User 2456424

                  Hi Richard MacCutchan! Follow me, the above code can be edited without using RunWorkerCompleted, the program still returns the same result because the PlaySync() method is a sequential command, simple the code will be easier to understand, you see the code below. RunWorkerCompleted event in other cases eg Play() instead PlaySync() method to catch "PLAY/STOP" status for example, do you think ? that is my opinion.

                  private void btnPlaySound_Click(object sender, EventArgs e)
                  {
                  lblStatusPlaySound.ForeColor = Color.Green;
                  lblStatusPlaySound.Text = "Status: PLAY";
                  Application.DoEvents();
                  Debug.Print("Status: PLAY");

                    using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
                    {
                        player.PlaySync();
                    }
                  
                    lblStatusPlaySound.ForeColor = Color.Red;
                    lblStatusPlaySound.Text = "Status: STOP";            
                    Application.DoEvents();         
                    Debug.Print("Status: STOP");
                  

                  }

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  Member 2458467 wrote:

                  Hi Richard MacCutchan!

                  Wrong Richard.

                  Member 2458467 wrote:

                  because the PlaySync() method is a sequential command

                  And that's the problem. The thread that calls PlaySync is blocked until the sound finishes playing. If you call it from the UI thread, your entire application will freeze, and you'll get the "Application is not responding" message if you try to interact with it. It might be OK for a very short sound, so long as you don't expect the UI to update whilst it's playing. But for anything longer than half a second, you need to play the sound from a background thread. Which is where the BackgroundWorker comes in. Sprinkling Application.DoEvents() calls throughout your code is a hack, and a sign of code which needs to be changed.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  U 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Member 2458467 wrote:

                    Hi Richard MacCutchan!

                    Wrong Richard.

                    Member 2458467 wrote:

                    because the PlaySync() method is a sequential command

                    And that's the problem. The thread that calls PlaySync is blocked until the sound finishes playing. If you call it from the UI thread, your entire application will freeze, and you'll get the "Application is not responding" message if you try to interact with it. It might be OK for a very short sound, so long as you don't expect the UI to update whilst it's playing. But for anything longer than half a second, you need to play the sound from a background thread. Which is where the BackgroundWorker comes in. Sprinkling Application.DoEvents() calls throughout your code is a hack, and a sign of code which needs to be changed.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    U Offline
                    U Offline
                    User 2456424
                    wrote on last edited by
                    #9

                    Ok, if not using PlaySync instead use mciSendString with status command, will this command play sequentially ?

                    Richard DeemingR 1 Reply Last reply
                    0
                    • U User 2456424

                      Ok, if not using PlaySync instead use mciSendString with status command, will this command play sequentially ?

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #10

                      As far as I can see, mciSendString doesn't wait for the sound to finish playing, so you'd be back to square one. And if it did wait, you'd still have to call it on a background thread to avoid freezing your UI. Just use a BackgroundWorker and the SoundPlayer class.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      U 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        As far as I can see, mciSendString doesn't wait for the sound to finish playing, so you'd be back to square one. And if it did wait, you'd still have to call it on a background thread to avoid freezing your UI. Just use a BackgroundWorker and the SoundPlayer class.


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        U Offline
                        U Offline
                        User 2456424
                        wrote on last edited by
                        #11

                        Thank you for answering my questions.

                        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