469,927 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Stop a thread (AT ONCE!)

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.
Nov 15 '05 #1
8 2071
"Tim Bücker" <ti***********@web.de> wrote in
news:br**********@news.uni-koblenz.de:
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------
Nov 15 '05 #2
"Peter Koen" <koen-newsreply&snusnu.at> schrieb im Newsbeitrag news:%2****************@TK2MSFTNGP10.phx.gbl...
"Tim Bücker" <ti***********@web.de> wrote in
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.

Nov 15 '05 #3
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" <ti***********@web.de> skrev i melding
news:br***********@news.uni-koblenz.de...
"Peter Koen" <koen-newsreply&snusnu.at> schrieb im Newsbeitrag

news:%2****************@TK2MSFTNGP10.phx.gbl...
"Tim Bücker" <ti***********@web.de> wrote in
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.

Nov 15 '05 #4
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" <os*@brdoadpark.no> schrieb im Newsbeitrag news:3f********@news.broadpark.no...
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" <ti***********@web.de> skrev i melding
news:br***********@news.uni-koblenz.de...
"Peter Koen" <koen-newsreply&snusnu.at> schrieb im Newsbeitrag

news:%2****************@TK2MSFTNGP10.phx.gbl...
"Tim Bücker" <ti***********@web.de> wrote in
> 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.


Nov 15 '05 #5
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" <ti***********@web.de> wrote in message
news:br**********@news.uni-koblenz.de...
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.

Nov 15 '05 #6
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

On Tue, 9 Dec 2003, it was written:
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" <ti***********@web.de> wrote in message
news:br**********@news.uni-koblenz.de...
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.


Nov 15 '05 #7
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
Nov 15 '05 #8
In C I do:

PlaySound (NULL, 0, SND_PURGE);

SND_PURGE = 0x40

regards,
scott

On Wed, 10 Dec 2003, Tim Bücker wrote:
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

Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by andrewcw | last post: by
4 posts views Thread by Osvaldo Bisignano | last post: by
3 posts views Thread by Ha ha | last post: by
1 post views Thread by magic man via .NET 247 | last post: by
51 posts views Thread by Hans | last post: by
27 posts views Thread by Ritesh Raj Sarraf | last post: by
2 posts views Thread by many_years_after | last post: by
3 posts views Thread by yeye.yang | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.