By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,888 Members | 1,232 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,888 IT Pros & Developers. It's quick & easy.

How to abort a thread?

P: n/a

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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.