Stop a thread (AT ONCE!)

  • Thread starter Thread starter Tim Bücker
  • Start date Start date
T

Tim Bücker

Following scenario:

The user opens a form, a thread is started to play a sound ...

[ThreadStart Method Example]
public void playSoundUsingThread()
{
if (File.Exists(fileLocation))
PlaySound(fileLocation, 0, 0); // winmm.dll
}

.... after that the user immediately closes the form and the music should stop at once!
But using thread.Abort(); doesn´t help as it gives the thread enough time to continue its "singing".

Is it possible to somehow stop the thread at once?
Thanks a lot for any information regarding this point!

Greetings,
Tim.
 
Following scenario:

The user opens a form, a thread is started to play a sound ...

[ThreadStart Method Example]
public void playSoundUsingThread()
{
if (File.Exists(fileLocation))
PlaySound(fileLocation, 0, 0); // winmm.dll
}

... after that the user immediately closes the form and the music
should stop at once! But using thread.Abort(); doesn´t help as it
gives the thread enough time to continue its "singing".

Is it possible to somehow stop the thread at once?
Thanks a lot for any information regarding this point!

Greetings,
Tim.

Hi Tim,

You won't be able to use this Function exactly how you want to use it.

In .NET it would be better to build a really nice multithreaded wrapper
around winmm.lib...

But there's a little trick you could use:

start the soundplayer on a second thread (background thread). And when
closing the app set the thread to null. this thorws immediately an
exception and cancels all threadactions.

greets
Peter

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD MCDBA
CAI/RS CASE/RS IAT

------ooo---OOO---ooo------
 
Peter Koen said:
Tim Bücker said:
Following scenario:

The user opens a form, a thread is started to play a sound ...

[ThreadStart Method Example]
public void playSoundUsingThread()
{
if (File.Exists(fileLocation))
PlaySound(fileLocation, 0, 0); // winmm.dll
}

... after that the user immediately closes the form and the music
should stop at once! But using thread.Abort(); doesn´t help as it
gives the thread enough time to continue its "singing".

Is it possible to somehow stop the thread at once?

First of all, thanks for the quick answer!
In .NET it would be better to build a really nice multithreaded wrapper
around winmm.lib...

Are there any resources about this in the web?
I´ll have a deeper look myself later - just in case you know a good one.
But there's a little trick you could use:

start the soundplayer on a second thread (background thread). And when
closing the app set the thread to null. this thorws immediately an
exception and cancels all threadactions.

Unfortunately, this trick is not working for me.
Perhaps it is because I don´t close the whole app just a simple dialog!?!

Greetings and thanks again for answering,
Tim.
 
After calling thread.Abort(), call thread.Join() immediately after. The
calling thread will then be halted until the sub thread has terminated. At
least your form shouldn't close until the sound has finished playing.

/OyvindS

Tim Bücker said:
"Peter Koen" <koen-newsreply&snusnu.at> schrieb im Newsbeitrag news:%[email protected]...
Tim Bücker said:
Following scenario:

The user opens a form, a thread is started to play a sound ...

[ThreadStart Method Example]
public void playSoundUsingThread()
{
if (File.Exists(fileLocation))
PlaySound(fileLocation, 0, 0); // winmm.dll
}

... after that the user immediately closes the form and the music
should stop at once! But using thread.Abort(); doesn´t help as it
gives the thread enough time to continue its "singing".

Is it possible to somehow stop the thread at once?

First of all, thanks for the quick answer!
In .NET it would be better to build a really nice multithreaded wrapper
around winmm.lib...

Are there any resources about this in the web?
I´ll have a deeper look myself later - just in case you know a good one.
But there's a little trick you could use:

start the soundplayer on a second thread (background thread). And when
closing the app set the thread to null. this thorws immediately an
exception and cancels all threadactions.

Unfortunately, this trick is not working for me.
Perhaps it is because I don´t close the whole app just a simple dialog!?!

Greetings and thanks again for answering,
Tim.
 
Hi.

Thanks for answering but what you described is exactly what I don´t want ;-)
I want that the user can close the form and then the sound should stop to play immediately!

Greetings,
Tim.

Oyvind said:
After calling thread.Abort(), call thread.Join() immediately after. The
calling thread will then be halted until the sub thread has terminated. At
least your form shouldn't close until the sound has finished playing.

/OyvindS

Tim Bücker said:
"Peter Koen" <koen-newsreply&snusnu.at> schrieb im Newsbeitrag news:%[email protected]...
Following scenario:

The user opens a form, a thread is started to play a sound ...

[ThreadStart Method Example]
public void playSoundUsingThread()
{
if (File.Exists(fileLocation))
PlaySound(fileLocation, 0, 0); // winmm.dll
}

... after that the user immediately closes the form and the music
should stop at once! But using thread.Abort(); doesn´t help as it
gives the thread enough time to continue its "singing".

Is it possible to somehow stop the thread at once?

First of all, thanks for the quick answer!
In .NET it would be better to build a really nice multithreaded wrapper
around winmm.lib...

Are there any resources about this in the web?
I´ll have a deeper look myself later - just in case you know a good one.
But there's a little trick you could use:

start the soundplayer on a second thread (background thread). And when
closing the app set the thread to null. this thorws immediately an
exception and cancels all threadactions.

Unfortunately, this trick is not working for me.
Perhaps it is because I don´t close the whole app just a simple dialog!?!

Greetings and thanks again for answering,
Tim.
 
Hi Tim,

Just to add that Abort won't stop unmanaged code - as you are seeing.
You could try playing song using managed DirectX extensions.
 
Could this also be caused by sound buffering in the library or the card?
You may want to halt the the play first then do your thread.Abort().

scott

Hi Tim,

Just to add that Abort won't stop unmanaged code - as you are seeing.
You could try playing song using managed DirectX extensions.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

Tim Bücker said:
Following scenario:

The user opens a form, a thread is started to play a sound ...

[ThreadStart Method Example]
public void playSoundUsingThread()
{
if (File.Exists(fileLocation))
PlaySound(fileLocation, 0, 0); // winmm.dll
}

... after that the user immediately closes the form and the music should stop at once!
But using thread.Abort(); doesn´t help as it gives the thread enough time to continue its "singing".

Is it possible to somehow stop the thread at once?
Thanks a lot for any information regarding this point!

Greetings,
Tim.
 
Thanks a lot for all the answers!
Perhaps I will have a deeper look at DirectSound.

@Scott:
Could this also be caused by sound buffering in the library or the card?
You may want to halt the the play first then do your thread.Abort().

If I could stop the music I wouldn´t have a problem as my thread ends with the "song".
But the problem is that it is not possible to halt the play using the method I am using at the moment!

Again thanks to all!
Greetings,
Tim
 
Back
Top