locked
background worker help needed RRS feed

  • Question

  • Hello

                         see the code below . I want sound to be play 4 time . but it plays only one.

     

     

     

    namespace WindowsApp

    {

        public partial class Form1 : Form

        {

            int counter = 0;

              System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer();

            public Form1()

            {

                InitializeComponent();

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

                for (int i = 0; i < 4; i++)

                {

     

                    if (!backgroundWorker1.IsBusy)

     

                        backgroundWorker1.RunWorkerAsync();

     

                    counter++;

     

                }

            }

     

     

     

            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

            {

                myPlayer.SoundLocation = @"Error.WAV";

     

                myPlayer.Play();

                counter--;

            }

     

            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

            {

                if (counter > 0)

                {

                    backgroundWorker1.RunWorkerAsync();

                }

     

            }

        }

    }


    It's Me
    Monday, May 30, 2011 7:09 AM

Answers

  • Your code acts as follows:

    It starts the loop --> Start playing first time --> i increases by one (=1) --> since backgroundworker is busy - it does not start the 2nd time

    -->  i increases by one (=2) --> since backgroundworker is busy - it does not start the 3rd time

    -->  i increases by one (=3) --> since backgroundworker is busy - it does not start the 4th time.

     

    In order for your code to work, change the code in Form1_Load:

          for (int i = 0; i < 4; i++)
          {
            while (backgroundWorker1.IsBusy)
             {
                 Application.DoEvents();
                 Thread.Sleep(1);
             } 
    
              backgroundWorker1.RunWorkerAsync();
    
             counter++;
          }
    

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Tuesday, May 31, 2011 8:34 PM
    • Marked as answer by Lie You Saturday, June 4, 2011 3:33 AM
    Tuesday, May 31, 2011 8:33 PM

All replies

  • What happens if you replace the code with this instead? I'm not sure if this is what you want though

      private void Form1_Load(object sender, EventArgs e)
        {
          backgroundWorker1.RunWorkerAsync();
        }
    
    
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
          myPlayer.SoundLocation = @"Error.WAV";
           for (int i = 0; i < 4; i++)
          { 
            myPlayer.Play();
          }
        } 
     
        
    


    Please mark this as answer or vote as helpful if it solved your problem
    Monday, May 30, 2011 7:26 AM
  • hello 

             many thanks  for reply it plays only onces (with out breakpoint).

     

    regards

     


    It's Me
    Monday, May 30, 2011 7:40 AM
  • Okay, it looks like the Play method plays the sound file Asynchronously. try calling

    myPlayer.PlayAsync();
    
    


    Please mark this as answer or vote as helpful if it solved your problem
    Monday, May 30, 2011 7:48 AM
  • Hello

                thanks for reply. there is no Async mthod 

     

    myPlayer.PlaySync();  

     

    but for sync my GUI hands b/c it run on user interface thread.


    It's Me
    Monday, May 30, 2011 8:48 AM
  • Hello,

    I think in this case the sound plays 4 times, but you can't hear that because it is too fast.

     

    Try put in the loop

    System.Threading.Thread.Sleep(...)

    to check how many times the sound plays.

     

    And this Sleep method shouldn't be in event _DoWork.

     

    Regards

    Adam




    Monday, May 30, 2011 8:54 AM
  • hello

                    I don't want to my GUI thread to sleep. that why i use play b/c it create new thread. Any advice


    It's Me
    Monday, May 30, 2011 9:32 AM
  • Use the BackgroundWorker and PlaySync.  Play plays asynchronously which means you're playing all instances simultaneously.
    Monday, May 30, 2011 9:57 AM
  • Hello,

     

    maybe try to use this code:

        public void start()
        {
          Thread newThread = new Thread(DoWorkForStarting);
          newThread.Start();
        }
    
    
    
        private void DoWorkForStarting(object o)
        {
          for (int i = 0; i < 4; i++)
          {
            Thread newThread = new Thread(DoWork);
            newThread.Start();
            newThread.Join();
          }
        }
    
        private void DoWork(object o)
        {
          //for example
          System.Threading.Thread.Sleep(1000);
          //your code for playing sound
          Console.WriteLine("I am playing a sound");
        }
    

    This is simple example.

    I hope that resolve your problem.

    Regards

    Adam

    Monday, May 30, 2011 10:18 AM
  • hello

                 I have tried every thing but still GUI hang . My somefunction will raise on some event in data i will get the message. somefunction will play the sound first time and add counter variable 

    background worker will run counter times loop and PlaySync the sound. but it counter is > 100 GUI hands

    see the code below 

    int counter =0;

    public void somefunction(string data)  // function will run on some event

    {

      string [] s = data.split[';'];

    foreach(string str in s)

    {

    if (!WAVbackgroundWorker.IsBusy)

        WAVbackgroundWorker.RunWorkerAsync();

     

    counter++

    }

    }

    private void WAVbackgroundWorker_DoWork(object sender, DoWorkEventArgs e)

            {

                for (int i = 0; i < counter; i++)

                {

                    myPlayer.SoundLocation = @"Error.WAV";

                    myPlayer.PlaySync();

                }

                counter= 0 ;

            }

     

            private void WAVbackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

            {

     

            }


    It's Me
    Tuesday, May 31, 2011 2:11 PM
  • hello

               thanks for reply. I don't want to use sleep  I have tried every thing but still GUI hang . My somefunction will raise on some event in data i will get the message. somefunction will play the sound first time and add counter variable 

    background worker will run counter times loop and PlaySync the sound. but it counter is > 100 GUI hands

    see the code below 

    int counter =0;

    public void somefunction(string data)  // function will run on some event

    {

      string [] s = data.split[';'];

    foreach(string str in s)

    {

     

    if (!WAVbackgroundWorker.IsBusy)

        WAVbackgroundWorker.RunWorkerAsync();

     

     

    counter++

    }

    }

     

    private void WAVbackgroundWorker_DoWork(object sender, DoWorkEventArgs e)

            {

                for (int i = 0; i < counter; i++)

                {

                    myPlayer.SoundLocation = @"Error.WAV";

                    myPlayer.PlaySync();

                }

                counter= 0 ;

            }

     

            private void WAVbackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

            {

     

            }

     



    It's Me
    Tuesday, May 31, 2011 2:23 PM
  • Don't play with counter from both threads.  If you want to change counter, supply it as a parameter in the RunWorkerAsync call.
    Tuesday, May 31, 2011 4:04 PM
  • Your code acts as follows:

    It starts the loop --> Start playing first time --> i increases by one (=1) --> since backgroundworker is busy - it does not start the 2nd time

    -->  i increases by one (=2) --> since backgroundworker is busy - it does not start the 3rd time

    -->  i increases by one (=3) --> since backgroundworker is busy - it does not start the 4th time.

     

    In order for your code to work, change the code in Form1_Load:

          for (int i = 0; i < 4; i++)
          {
            while (backgroundWorker1.IsBusy)
             {
                 Application.DoEvents();
                 Thread.Sleep(1);
             } 
    
              backgroundWorker1.RunWorkerAsync();
    
             counter++;
          }
    

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Tuesday, May 31, 2011 8:34 PM
    • Marked as answer by Lie You Saturday, June 4, 2011 3:33 AM
    Tuesday, May 31, 2011 8:33 PM