473,797 Members | 2,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.In terrupt() 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_objAudioRecor derPlayer.OpenF ile(m_arrFiles( intLoopCounter) )
m_objAudioRecor derPlayer.Start Playing(m_lngPl ayFromTime,
m_lngPlayToTime )
m_PlayThread.Sl eep(lngPlayToTi me - lngPlayFromTime )
If m_IsStop Then
m_objAudioRecor derPlayer.Close File()
End If
Catch Ex As Exception
m_objAudioRecor derPlayer.Close File()
End Try
Loop
End Sub

Private Sub cmdPlay_Click(S ender As Object, ------)
Dim m_PlayThread as New Threading.Threa d(AddressOf PlayAudio)
m_PlayThread.St art()
End Sub

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

Private Sub cmdResume_Click (--------)
m_PlayThread.Re sume()
End Sub

Private Sub cmdStop_Click(--------)
m_IsStop = True
If m_PlayThread.Th readState = WaitSleepJoin Then
m_PlayThread.In terrupt()
End If
End Sub

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


Nov 16 '05 #1
1 6454
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******@annet site.com> wrote in message
news:eY******** ******@TK2MSFTN GP14.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.In terrupt() 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_objAudioRecor derPlayer.OpenF ile(m_arrFiles( intLoopCounter) )
m_objAudioRecor derPlayer.Start Playing(m_lngPl ayFromTime,
m_lngPlayToTime )
m_PlayThread.Sl eep(lngPlayToTi me - lngPlayFromTime )
If m_IsStop Then
m_objAudioRecor derPlayer.Close File()
End If
Catch Ex As Exception
m_objAudioRecor derPlayer.Close File()
End Try
Loop
End Sub

Private Sub cmdPlay_Click(S ender As Object, ------)
Dim m_PlayThread as New Threading.Threa d(AddressOf PlayAudio)
m_PlayThread.St art()
End Sub

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

Private Sub cmdResume_Click (--------)
m_PlayThread.Re sume()
End Sub

Private Sub cmdStop_Click(--------)
m_IsStop = True
If m_PlayThread.Th readState = WaitSleepJoin Then
m_PlayThread.In terrupt()
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
2777
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 halted. If a socket is no longer in the configuration, that thread must be aborted and the socket must be closed. After the configuration is complete, the remaining threads must be resumed. I currently have the following architecture:
16
2052
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 before calling itself recursively, which means it is supposed to run on the hour every hour. But it automatically stops after 2 hours. I only get 2 emails, 1 per hour and then nothing. When I examine the threadstate during run-time, it has changed...
8
36466
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 seems to work. But I have no clue how to pause/suspend the process. As far as I can see the Process class doesn't offer anything for this. So it's probably the thread the process is running on that should be suspended or put to sleep. But just...
14
2326
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 it has been suspended. The C# iterators seem to be a special case of this general suspend/resume concept. The "yield" statement suspends the execution of the current method and calling MoveNext() resumes it. I think it would be cleaner to...
1
1656
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 thread-to-be-suspended to log the fact that it is being suspended. Any ideas?
4
11110
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, how can I do? Thanks a lot!
2
2302
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 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.
2
1636
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 times it might be necessary to pause execution of these threads. Thread.Suspend() looks like an excellent candidate, but the method is deprecated in the framework. According to the documentation I should use dedicated objects like Monitors for...
6
1418
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 but using it under certain circumstances still seems reasonable. For instance, I want to display a small modal dialog with a "Cancel" button which allows the user to abort a background thread. If the user clicks this button, I then want to prompt...
3
6061
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 different thread other than the treadToDump the threadToDump must be suspended otherwise a ThreadStateException gets thrown. However under .NET 2.0 System.Threading.Thread.Suspend/Resume have been marked as obsolete with reference to better methods of...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9536
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10468
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10245
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9063
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.