How to abort a thread?

  • Thread starter Thread starter Sakharam Phapale
  • Start date Start date
S

Sakharam Phapale

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
 
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 said:
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.
 
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,

I hope this gives some ideas?

Cor
 
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_strTempWaveFileName)



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(lngPlayFromTime,
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
 
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,
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/d..._vbcode/html/vbtskcodeexampleplayingsound.asp

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

Hope this helps
Jay
 
Back
Top