How to stop the Threadmill

qznc qznc at go.to
Mon Jan 28 02:30:34 PST 2013


On Monday, 28 January 2013 at 10:18:27 UTC, Chris wrote:
> I am sure this has been asked before, but I couldn't find a 
> solution or a hint via Google: I use a separate thread to play 
> a sound file. Everything works fine, except that I cannot tell 
> the thread to stop what it is doing. It refuses to receive 
> messages while it is doing what it's doing (even if I just run 
> a simple test function with a long for-loop to make sure it's 
> not a hardware issue). I should probably add that the thread is 
> started from within the main loop of a socket that waits for 
> input. If the input is "stop" it should tell the play-thread to 
> stop, but nothing happens.
> I can imagine that the thread is no longer visible to the 
> main-thread, although I keep a reference to it. Any help or 
> hint will be appreciated.

There is no Thread.kill/stop method, if you are looking for it.

Your thread should run a loop to check for termination repeatedly.

   while (this.running) {
     // do another chunk of work
   }

To terminate the thread, you need to toggle the running flag and 
wait for it to end.

   soundplayer.running = false;

If your "chunk of work" is blocked as it waits for IO or 
something, that might take a long time.


More information about the Digitalmars-d-learn mailing list