473,382 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 2294
"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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.