How to suspend a thread which is in WaitSleepJoin state.

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

Sakharam Phapale

Hi All,

In following example, while playing file, if thread goes in WaitSleepJoin
state, due to Thread.Sleep method.
Now I want to suspend thread by clicking on cmdSuspend button. I have
written following code, which doesn't suspend it.
Even I tried it by calling m_PlayThread.Interrupt() method which suspends
the thread but cannot resume it again by calling Resume method.

Any kind help will be appreciated.

Thanks and Regards
Sakharam Phapale

----------------------------------------------------------------------------
-----------------------

Private m_IsStop As Boolean

Private sub PlayAudio()
m_IsStop = False
For intLoopCounter = 0 To m_intNoOfFiles
Try
m_objAudioRecorderPlayer.OpenFile(m_arrFiles(intLoopCounter))
m_objAudioRecorderPlayer.StartPlaying(m_lngPlayFromTime,
m_lngPlayToTime)
m_PlayThread.Sleep(lngPlayToTime - lngPlayFromTime)
If m_IsStop Then
m_objAudioRecorderPlayer.CloseFile()
End If
Catch Ex As Exception
m_objAudioRecorderPlayer.CloseFile()
End Try
Loop
End Sub

Private Sub cmdPlay_Click(Sender As Object, ------)
Dim m_PlayThread as New Threading.Thread(AddressOf PlayAudio)
m_PlayThread.Start()
End Sub

Private Sub cmdPause_Click(sender as Object,-------)
m_PlayThread.Suspend()
End Sub

Private Sub cmdResume_Click(--------)
m_PlayThread.Resume()
End Sub

Private Sub cmdStop_Click(--------)
m_IsStop = True
If m_PlayThread.ThreadState = WaitSleepJoin Then
m_PlayThread.Interrupt()
End If
End Sub
 
Sakharam,
What are you expecting Thead.Suspend to suspend?

The playing of the music? It cannot do this, as you using the
objAudioRecorderPlayer object to control playing of the music, you don't
tell us what the objAudioRecorderPlayer is, but I suspect it has its own
thread to play the music (you are using a thread to control a thread, to
control yet another thread, and you are one thread away from where you need
to be...)

Thread.Interrupt actually wakes up a thread, by causing a
ThreadInterruptedException to be raised on that thread.

I would expect the objAudioRecorderPlayer to have methods to suspend,
resume, and stop playback of the music. The trick is going to be is you need
to suspend both your thread & objAudioRecorderPlayer, resume both
objAudioRecorderPlayer & your thread, plus stop your thread &
objAudioRecorderPlayer...

As the thread the Cor suggested, I would review other playback methods other
then objAudioRecorderPlayer. Or preferable I would research
objAudioRecorderPlayer to learn how to have it playback sounds without an
unneeded thread in my app, as I strongly suspect it already is doing all the
threading it needs via some ASYNC option.

As I stated in the other thread:

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

If I needed to use a thread in my app, then I would use the PlaySounds API
directly without having the objAudioRecorderPlayer object in the way...

Hope this helps
Jay
 
Back
Top