How to suspend a thread which is in WaitSleepJoin state.

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

----------------------------------------------------------------------------
 
D

David Levine

I haven't looked at your code; this is just a general approach I take. I
never use suspend/resume, and IMO these should not have even been put into
the API, or made it part of a different class. Instead, use synchronization
mechanisms, such as Monitor, mutex, reader/writer locks, or semaphores, and
control the execution of the thread by waiting/blocking on the sync object
in the appropriate place. Getting suspend/resume logic to work correctly is
non-trivial, especially since you tend to get more involved in the internals
of the thread itself (e.g. is it in a state where resuming a thread will
have an effect?). For general purpose apps, blocking is usually more then
enough, certainly so for the simple task you want to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top