472,358 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

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 2220
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: andrewcw | last post by:
I have an external VB6 app that I call in a loop. Because the process is long, I would like to be able to terminate the spawned thread & the VB6 application. My current loop calls a this...
4
by: Osvaldo Bisignano | last post by:
When I press a button, I start a new Exe file as a new Thread. When the mainform of that exe closes, i thought the thread was over. When and How shoud i terminate the subprocess?
3
by: Ha ha | last post by:
I create a main thread ,than create a subthread in the main thread,when i found any error,i want to stop the subthread and the main thread immediatly .How can i do for it. *** Sent via...
1
by: magic man via .NET 247 | last post by:
hi everyone i have a c# application that uses multithreading toconnect to sql server and execute a stored procedure on theserver. i am using a dataset,sqlcommand,dataadapter and adatagrid to carry...
1
by: scorpion53061 | last post by:
I have MS Word operating in a thread other than the main writing a report. Can I tell the main thread to wait until a particular point (a sub starts) in another thread before continuing on?
51
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() ...
27
by: Ritesh Raj Sarraf | last post by:
Hi, I have some basic doubts about thread. I have a list which has items in it which need to be downloaded from the internet. Let's say list is: list_items which has 100 items in it.
2
by: many_years_after | last post by:
Hi,pythoners: I countered some problems when I try to stop threads using flag. These are my some important codes: ##### mythread.py def run(self): while self.addr != '': ### text waiting...
3
by: yeye.yang | last post by:
hey everybody Does everybody can help me or give me some advise for the cross thread exception catch Here is what I want to do: I have 2 classes "Scenario" and "Step", which have a...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.