473,386 Members | 1,819 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,386 software developers and data experts.

How to abort a thread?


Private myThread As Thread

Sub cmdStart_Click()
myThread = New Thread(AddressOf PlayAudio)
myThread.Start
End Sub

Sub PlayAudio()
Open wave file
Play wave file
Close wave file
Thread.Abort
End Sub

Sub cmdStop_Click()
Close wave file
Thread.Abort
End Sub

Here when I press STOP button, wave file doesn't get closed, because it is
being used by another process (myThread).
Even I tried it by opening wave file in START button click event. But then
file doesn't played.
Can anyone tell me how to achieve above stuff.

Thanks and Regards
Sakharam Phapale

Nov 21 '05 #1
6 2239
Hi Sakharam,

you could use thread.Abort but only in the Stop button event.
Don't use it inside of the thread method.

I hope that helps.

I have tried your sample and is ok, but remember that you hace to change:

Sub PlayAudio()
Open wave file
Play wave file
Close wave file
End Sub
Kind Regards,

Jorge Serrano
MVP VB.NET
"Sakharam Phapale" wrote:

Private myThread As Thread

Sub cmdStart_Click()
myThread = New Thread(AddressOf PlayAudio)
myThread.Start
End Sub

Sub PlayAudio()
Open wave file
Play wave file
Close wave file
Thread.Abort
End Sub

Sub cmdStop_Click()
Close wave file
Thread.Abort
End Sub

Here when I press STOP button, wave file doesn't get closed, because it is
being used by another process (myThread).
Even I tried it by opening wave file in START button click event. But then
file doesn't played.
Can anyone tell me how to achieve above stuff.

Thanks and Regards
Sakharam Phapale

Nov 21 '05 #2
"Sakharam Phapale" <sp******@annetsite.com> schrieb:
Private myThread As Thread

Sub cmdStart_Click()
myThread = New Thread(AddressOf PlayAudio)
myThread.Start
End Sub

Sub PlayAudio()
Open wave file
Play wave file
Close wave file
Thread.Abort
End Sub

Sub cmdStop_Click()
Close wave file
Thread.Abort
End Sub

Here when I press STOP button, wave file doesn't get closed, because it is
being used by another process (myThread).


Using 'Thread.Abort' is not a good idea. Instead set a boolean variable,
for example, that is checked by the thread, and the thread terminates by
returning if it is set.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #3
Sakharam,

Show us as well how you start the wav file, when it is with a process.start
it will in my opinion not help you much using the thread. Than that process
will probably directly be started and the mythread directly close.

Beside that some comments inline
Private myThread As Thread

Sub cmdStart_Click()
myThread = New Thread(AddressOf PlayAudio)
myThread.Start
End Sub

Sub PlayAudio()
Open wave file
Play wave file
Close wave file
Thread.Abort This one above is useless End Sub

Sub cmdStop_Click()
Close wave file This one above should be impossible Thread.Abort With this you mean probably
MyThread.abort, End Sub


I hope this gives some ideas?

Cor
Nov 21 '05 #4

Hi All,

This is snippet of my code.
Private Sub cmdStart_Click ()

m_lngPlayFromElementNo = 5

m_lngPlayToElementNo =15

m_myPlayThread = New Thread(AddressOf PlaySelectedAudio)

m_myPlayThread.Start()

End Sub

Private Sub PlaySelectedAudio()

Dim lngCounter As Long

m_blnIsPlayStop = False

m_objAudioRecorderPlayer.OpenExistingFile(m_strTem pWaveFileName)

For lngCounter = m_lngPlayFromElementNo To m_lngPlayToElementNo

Dim lngPlayFromTime As Long = 0

Dim lngPlayToTime As Long = 0

lngPlayFromTime = Val(objWordData.arrWordDataTable(lngCounter, 6)) *
10

lngPlayToTime = Val(objWordData.arrWordDataTable(lngTempCounter, 6))
* 10

m_objAudioRecorderPlayer.StartPlaying(lngPlayFromT ime,
lngPlayToTime)

m_myPlayThread.Sleep((lngPlayToTime - lngPlayFromTime))

' Start playing and wait until complete.

If m_blnIsPlayStop = True Then GoTo StopThread

Next

StopThread:

m_objAudioRecorderPlayer.StopPlaying()

m_objAudioRecorderPlayer.CloseFile()

End Sub

Public Sub cmdStop_Click()

m_blnIsPlayStop = True

End Sub
I am using "mciSendString" API for Audio functionality.

While control is in Thread and file is playing myThread is in sleep state.
Now if user clicks on STOP button thread should stop playing wave file.

Please tell me how to do that.
Thank all of you for your reply.

Thanks and Regards,
Sakharam Phapale


"Cor Ligthert" <no************@planet.nl> wrote in message
news:eH**************@TK2MSFTNGP11.phx.gbl...
Sakharam,

Show us as well how you start the wav file, when it is with a process.start it will in my opinion not help you much using the thread. Than that process will probably directly be started and the mythread directly close.

Beside that some comments inline
Private myThread As Thread

Sub cmdStart_Click()
myThread = New Thread(AddressOf PlayAudio)
myThread.Start
End Sub

Sub PlayAudio()
Open wave file
Play wave file
Close wave file
Thread.Abort

This one above is useless
End Sub

Sub cmdStop_Click()
Close wave file

This one above should be impossible
Thread.Abort

With this you mean probably
MyThread.abort,
End Sub


I hope this gives some ideas?

Cor

Nov 21 '05 #5
Shakharam

I know that Larry had a kind of same problem some weeks ago using I thought
the same code as you, maybe he has direct your answer, at the time he asked
it, I could not give the answer direct as well.

Probably he read this newsgroup in for me the afternoon, I have put his name
in the subject.

Cor
"Sakharam Phapale" <sp******@annetsite.com> > Hi All,

This is snippet of my code.
Private Sub cmdStart_Click ()

m_lngPlayFromElementNo = 5

m_lngPlayToElementNo =15

m_myPlayThread = New Thread(AddressOf PlaySelectedAudio)

m_myPlayThread.Start()

End Sub

Private Sub PlaySelectedAudio()

Dim lngCounter As Long

m_blnIsPlayStop = False

m_objAudioRecorderPlayer.OpenExistingFile(m_strTem pWaveFileName)

For lngCounter = m_lngPlayFromElementNo To m_lngPlayToElementNo

Dim lngPlayFromTime As Long = 0

Dim lngPlayToTime As Long = 0

lngPlayFromTime = Val(objWordData.arrWordDataTable(lngCounter, 6))
*
10

lngPlayToTime = Val(objWordData.arrWordDataTable(lngTempCounter,
6))
* 10

m_objAudioRecorderPlayer.StartPlaying(lngPlayFromT ime,
lngPlayToTime)

m_myPlayThread.Sleep((lngPlayToTime - lngPlayFromTime))

' Start playing and wait until complete.

If m_blnIsPlayStop = True Then GoTo StopThread

Next

StopThread:

m_objAudioRecorderPlayer.StopPlaying()

m_objAudioRecorderPlayer.CloseFile()

End Sub

Public Sub cmdStop_Click()

m_blnIsPlayStop = True

End Sub
I am using "mciSendString" API for Audio functionality.

While control is in Thread and file is playing myThread is in sleep state.
Now if user clicks on STOP button thread should stop playing wave file.

Please tell me how to do that.
Thank all of you for your reply.

Thanks and Regards,
Sakharam Phapale


"Cor Ligthert" <no************@planet.nl> wrote in message
news:eH**************@TK2MSFTNGP11.phx.gbl...
Sakharam,

Show us as well how you start the wav file, when it is with a

process.start
it will in my opinion not help you much using the thread. Than that

process
will probably directly be started and the mythread directly close.

Beside that some comments inline
> Private myThread As Thread
>
> Sub cmdStart_Click()
> myThread = New Thread(AddressOf PlayAudio)
> myThread.Start
> End Sub
>
> Sub PlayAudio()
> Open wave file
> Play wave file
> Close wave file
> Thread.Abort

This one above is useless
> End Sub
>
> Sub cmdStop_Click()
> Close wave file

This one above should be impossible
> Thread.Abort

With this you mean probably
MyThread.abort,
> End Sub
>


I hope this gives some ideas?

Cor


Nov 21 '05 #6
Sakharam,
In addition to the other comments, be certain to read the comments on
Thread.Abort specifically "If, while executing unmanaged code, a thread
ignores a ThreadAbortException, the system re-throws the
ThreadAbortException when the thread begins executing managed code."

In other words if you are using a Win32 API or ActiveX control to play the
Audio, Thread.Abort will not occur until the method to play said Audio has
returned. You can use Thread.Join to wait for the second thread to finish...
Also, do you really need a thread to play a sound in the background? I know
you can use the Win32 PlaySound API and pass it SND_ASYNC to have it play
the sound in the background. For details on PlaySound see:
http://msdn.microsoft.com/library/de...ayingsound.asp

I would think (hope) what ever you are using to play sounds has a similar
asynchronous setting.

Hope this helps
Jay

"Sakharam Phapale" <sp******@annetsite.com> wrote in message
news:OU**************@TK2MSFTNGP14.phx.gbl...

Private myThread As Thread

Sub cmdStart_Click()
myThread = New Thread(AddressOf PlayAudio)
myThread.Start
End Sub

Sub PlayAudio()
Open wave file
Play wave file
Close wave file
Thread.Abort
End Sub

Sub cmdStop_Click()
Close wave file
Thread.Abort
End Sub

Here when I press STOP button, wave file doesn't get closed, because it is
being used by another process (myThread).
Even I tried it by opening wave file in START button click event. But then
file doesn't played.
Can anyone tell me how to achieve above stuff.

Thanks and Regards
Sakharam Phapale

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Keith Langer | last post by:
I have an application which consists of a main work thread and multiple threads which each maintain a TCP socket. When a configuration change occurs, all activity on the socket threads must be...
14
by: Daisy | last post by:
From this page: http://www.c-sharpcorner.com/2/mt_beginner1.asp Thread class's Abort method is called to kill a thread permanently. Make sure you call IsAlive before Abort. if (...
7
by: Morris | last post by:
I want to abort a running thread, so I call MyThread.abort() function. My problem is this thread runs "almost" like a while(true) loop and I don't want the Abort() function interrupts the thread at...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
5
by: [Yosi] | last post by:
Why I can't abot a susspended thread. Who can terminat a thread imediatly without consider to its stat or execution?
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
4
by: fred | last post by:
I use a Synclock in a secondary thread and also stop the thread using the abort method. If the abort occurs while the thread is in the Synclock will the SyncLock always be released before the...
6
by: Joe HM | last post by:
Hello - I have a function that calls Thread.Abort() to stop a thread in a _Closed() Method of a GUI. The thread contains a blocking call on a TCP socket and that is the easiest way to stop...
5
by: andrew | last post by:
Hi, I have the following issue with the Thread.Abort(): The main thread creates a worker thread which waits on a process termination. void ThreadProc() { Process proc =...
6
by: mehdi | last post by:
Hi folks, You know, the Thread class has got a method named Abort which according to the msdn: "Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.