locked
Thread help needed RRS feed

  • Question

  • Hello

                  I want WAV file which i have to play on different thread when i received event . Since i am getting event quickly while the pervious event file is palying i can't play again. see the below code 

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

    {

                                                      if(!WAVbackgroundWorker.IsBusy)

                                                         WAVbackgroundWorker.RunWorkerAsync();

    }

    private void WAVbackgroundWorker_DoWork(object sender, DoWorkEventArgs e)

            {

                myPlayer.SoundLocation = @"Error.WAV";

                myPlayer.Play();

            }

     

      Note I want  play it 4 time with out thread sleep.


    It's Me
    Friday, May 27, 2011 11:55 AM

All replies

  • Hello, you can't use RunWorkerAsync method inside a loop because the method call is asynchronous, then the loop will continue the execution and your sound only play once. If you need to play it 4 times, you can use the RunWorkerCompleted event and re-call the RunWorkerAsync() again. You can put an external counter and decrease it inside the DoWork event handler so you can check in the RunWorkerCompleted if that counter if greater than 0 to play again the sound.

    Regards,

    Miguel.

    Friday, May 27, 2011 12:40 PM
  • hello

                   thanks for reply.well I am listen data of the socket.  data contain product id , descriptaion , stocklimit. etc. Now i want to play sound when i got the particular product data who stock limit is less then the requried limit.

     

    Now there are 100000 of product in the stock.   I don't know in advance how many product limit is less then the requried limit.   some time 1 message in 1 millisecond and some then 5 in 5 millisecond. 10 , 20 50 , so on . NOW i want to play sound for evey message.

     

    in my case if i got let say 4 message sound play only once. i want to play sound of every message who limit is less the requried . using thread so that GUI does not hand.

     

    regards

     


    It's Me
    Sunday, May 29, 2011 7:34 AM
  • Hi,

    I think you need to implement some more logic here (dependend on what you try to achieve exactly):

    1) When you want to use a background worker, then you should simply check first, if the worker is already running. If so, there is no need to start it again.

    2) If you want to be able to play the sounds one after the other, you could also implement a (thread safe!) queue where you put one sound after the other.

    So the main logic in words is:
    - Put the new sound in the queue
    - Start the worker if it is not running

    The worker just checks the list and is playing the sounds in the queue one after the other till the queue is empty. Then the thread will stop.

    I hope this helped a little.

    With kind regards,

    Konrad

    Sunday, May 29, 2011 11:42 AM
  • hello

              Many thanks for reply. what if i use timer and every time i get the message i will play that sound.  timer will run in the background .

     

    Any idea

     

    regards

     


    It's Me
    Sunday, May 29, 2011 5:56 PM
  • What is the requirement? I understood, that you get an event from somewhere and then you want to play a sound. A Timer is giving you events at defined times. So that is not usefull at all from what I understood so far.

    But of course: Somehow you could do something with a timer but I wouldn't consider it a straight forward solution.

    But it could be that I simply misunderstood the requirements.

    With kind regards,

    Konrad

    Sunday, May 29, 2011 5:59 PM
  • Hello

                 thanks for reply. see the code below it play only ones 

     

     

    approach 1 ...................................

    public void Add () raise on some event..

    {

     

    lock (QueueDeadSound)

                          {

                        QueueDeadSound.Enqueue(@"Error.WAV");

                    ObjAutoResetEvent.Set();  // start when queue has data.

              } 

     

    }

     

    /////////////////

     

      void Dispatch()

            {

                while (true)

                {

                    if (QueueDeadSound.Count > 0)

                    {

                        lock (QueueDeadSound)

                        {

                            myPlayer.SoundLocation = QueueDeadSound.Dequeue();

                            // myPlayer.SoundLocation = @"Error.WAV";

                            myPlayer.Play();

                        }

                    }

                    else

                    {

                        ObjAutoResetEvent.WaitOne();  // wait till queue has on data.

                    }               

                }

            }

     

     

    Approach 2 ....................................

    public void Add () raise on some event..

    {

     

    QueueDeadSound.Enqueue(@"Error.WAV");

                                                      if (!WAVbackgroundWorker.IsBusy)

                                                      {

     

                                                          WAVbackgroundWorker.RunWorkerAsync(compountantName);

                                                      }

    }

     

     private void WAVbackgroundWorker_DoWork(object sender, DoWorkEventArgs e)

            {

                lock (QueueDeadSound)

                {

                    myPlayer.SoundLocation = QueueDeadSound.Dequeue();

                }

     

                myPlayer.Play();

            }

     

     

    In both the cases sound is played only onces .... please help 

     


    It's Me
    Monday, May 30, 2011 6:27 AM