473,778 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 playSoundUsingT hread()
{
if (File.Exists(fi leLocation))
PlaySound(fileL ocation, 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 2311
"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 playSoundUsingT hread()
{
if (File.Exists(fi leLocation))
PlaySound(fileL ocation, 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&snusn u.at> schrieb im Newsbeitrag news:%2******** ********@TK2MSF TNGP10.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 playSoundUsingT hread()
{
if (File.Exists(fi leLocation))
PlaySound(fileL ocation, 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&snusn u.at> schrieb im Newsbeitrag

news:%2******** ********@TK2MSF TNGP10.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 playSoundUsingT hread()
{
if (File.Exists(fi leLocation))
PlaySound(fileL ocation, 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&snusn u.at> schrieb im Newsbeitrag

news:%2******** ********@TK2MSF TNGP10.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 playSoundUsingT hread()
> {
> if (File.Exists(fi leLocation))
> PlaySound(fileL ocation, 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 playSoundUsingT hread()
{
if (File.Exists(fi leLocation))
PlaySound(fileL ocation, 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 playSoundUsingT hread()
{
if (File.Exists(fi leLocation))
PlaySound(fileL ocation, 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
1017
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 function to create and start a new thread. I suspect I can make it 'global' and then simply restart the thread. Instead of making a new one, but will that stop the VB app ? And is this a good way to do it ? Thanks
4
3350
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
5957
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 Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
1
2248
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 out the process on a background thread andeverything goes well but the problem arrises when i created astop button that attempts to cancel the operation of datasetfilling(somtimes the query takes much time and i need to cancelthe operation) so that...
1
1553
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
54877
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() # This thread is expected to finish within a second
27
1740
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
3148
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 for processing if not self.CONTINUE: ### flag for thread ,means to exit the RUN func or not
3
3181
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 System.Thread.Timer for each to control their timeout gestion, in my "main program", I start the "Scenario" and "Step", and I do something between "StepStart" and "StepStop", when "Scenario" or "Step" timeout expired, they raise an "exception", then my "main...
0
9629
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9465
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10068
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2863
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.