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

How to suspend a thread which is in WaitSleepJoin state.

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(intLo opCounter))
m_objAudioRecorderPlayer.StartPlaying(m_lngPlayFro mTime,
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

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


Nov 16 '05 #1
1 6423
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.
"Sakharam Phapale" <sp******@annetsite.com> wrote in message
news:eY**************@TK2MSFTNGP14.phx.gbl...
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(intLo opCounter))
m_objAudioRecorderPlayer.StartPlaying(m_lngPlayFro mTime,
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

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

Nov 16 '05 #2

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...
16
by: Alvin Bruney | last post by:
I'm observing that a sleeping thread changes to stopped after a while. Is that accepted framework behavior for web applications? My thread basically does some work, and sleeps for 60 minutes...
8
by: Wim | last post by:
My GUI application starts a process (a console program) when the user hits Play. I would like to add an option to pause that process. The code I've added to detect if the user hit pause/unpause...
14
by: Jiri Kripac | last post by:
Languages such as Simula 67 contain a general concept of coroutines that allow the execution of a method to be suspended without rolling back the stack and then later resumed at the same place as...
1
by: Paul Tomlinson | last post by:
Quick question. If i call Thread.suspend() on a thread does the thread which is being suspended get any notification that it has been/is being suspended? I am thinking i want the...
4
by: am | last post by:
Hi to all. I have a little problem. I'm working with threads, and I need to abort or suspend them, but many experts dissuade from use Thread.Abort and Thread.Suspend. As I didn't find other way,...
2
by: Sakharam Phapale | last post by:
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...
2
by: b.fokke | last post by:
Hi all! I'm working on an application that due to the nature of the application is heavily concurrent. Usually, there will be between 15 and 20 threads running at the same time. However, at some...
6
by: Robert Speck | last post by:
Hi there, Can anyone shed anymore light on why "Thread.Suspend()" has been deprecated by MSFT beyond what MSDN says about it. I'm not sure if I quite appreciate the various pitfalls they discuss...
3
by: =?Utf-8?B?TWFyayBDaGFubmluZw==?= | last post by:
I have a code which registers all threads with a thread dump class. At intervals this thread dump class will dump the stack trace of all threads. As calling StackTrace(threadtoDump) from a...
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.