Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7525831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:39:40+00:00 2026-05-30T03:39:40+00:00

The code in question public void StartPlaying() { ThreadPool.QueueUserWorkItem(ignoredState => { while (_playlist.Count >

  • 0

The code in question

public void StartPlaying()
{
    ThreadPool.QueueUserWorkItem(ignoredState =>
    {
        while (_playlist.Count > 0)
        {
            var audioFile = _playlist.Dequeue();

            if (StartedPlaying != null)
                StartedPlaying(this, new TypedAudioFileEventArgs(audioFile));

            audioFile.SoundPlayer.PlaySync();
            audioFile.SoundPlayer.Dispose();

            if (StoppedPlaying != null)
                StoppedPlaying(this, new TypedAudioFileEventArgs(audioFile));
        }
    });
}

and my test:

[TestMethod()]
public void StartPlayIsCalledTwice_OnlyRunningOnce()
{
    int timeBetweenPlays = 0; 
    var target = new TypedAudioFilePlayer(timeBetweenPlays);

    target.AddFile(TypedAudioFileCreator.CreateWord(1, "bl"));
    target.StartedPlaying += StartedPlaying_Once;

    target.StartPlaying();
    target.StartPlaying();
}

private bool _once = false;
private void StartedPlaying_Once(object sender, TypedAudioFileEventArgs e)
{
    if (!_once)
        _once = true;
    else
        Assert.Fail("Should not be called more than once!");
}

I believe my unit test should fail, judging by the MSDN description of ThreadPool.QueueUserWorkItem:

Queues a method for execution. The method executes when a thread pool thread becomes available.

The default ThreadPool size is 512, so two threads should immediately be available to process the StartPlaying call. I believe my code should fail since I haven’t provided any safeguards from race conditions in which both threads can access the same resource.

What’s happening here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:39:42+00:00Added an answer on May 30, 2026 at 3:39 am

    Because the StartedPlaying event is only raised if StartPlaying is called when there are items to play.

    _playlist.Dequeue(); dequeues the file you enqueue. Therefore the second time you get to while (_playlist.Count > 0) it will immediately fail, passing the second call to StartPlaying straight through without raising the event.

    Also, as Bruno Silva points out, the thread spawned by the second call to StartPlaying may not have a chance to execute anything before the test exits.

    For what it’s worth, there are about a million at least 2 threading mistakes in this code also:

    // Where did _playlist come from?  Is it shared state among the player threads?
    // If so, all access to it should be in locks, since queues are not thread safe
    while (_playlist.Count > 0) 
    
    
    // Both of these start threads and then immediately return.  
    // The test will probably exit before either of those threads do anything much
        target.StartPlaying();
        target.StartPlaying();
    }
    

    If you want to properly unit test, you need to define preconditions, expectations, actions, and postconditions:

    • Preconditions: you have an initialized TypedAudioFilePlayer with one file queued:

      var target = new TypedAudioFilePlayer(timeBetweenPlays);
      target.AddFile(TypedAudioFileCreator.CreateWord(1, "bl"));

    • Expectations: The StartedPlaying event will be raised only once if StartPlaying is called twice

      target.StartedPlaying += StartedPlaying_Once;

    • Actions: The StartPlaying method will be called twice:

      target.StartPlaying();
      target.StartPlaying();

    • Postconditions: The StartedPlaying event was only raised once:

      private bool _once = false;

      private void StartedPlaying_Once(object sender, TypedAudioFileEventArgs e)
      {
      if (!_once)
      _once = true;
      else
      Assert.Fail("Should not be called more than once!");
      }

    Now, your test succeeds. That’s not good in this case, because of what I explain above. You need to get your test to a failing state by eliminating the queue bug and race condition, then work on making the test pass the right way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with a contentPane. Here's the code in question: public void graph()
At the moment I am using the following code: public void init() { question
The following code demonstrates my question: public class DynamicExample { public void DoSomething() {
This code works fine [Test] public void boo() { var collection = new[] {
Simple question, how make this code working ? public class T { public static
The code in question is here: var $item = $(this).parent().parent().find('input'); What is the purpose
I found this code on internet : Class Book{ Public: void operator()(int Counter) const
I have a code that looks like (more or less) : public void INeedHolidaysNow(double[,]){
this i a snippet of my code.... public void btn_browse_Click_Click(object sender, EventArgs e) {
I have the following code (OLE = OptimisticLockException) ... public void outer() { try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.