Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2007

Tip: Looking for answers? Try searching our database.

How to pause another thread?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Néstor Sánchez A. - 27 Jun 2007 16:31 GMT
Hi,
i'm writing a windows service for wich a need to run an auxiliary thread.
When the service will get paused i need to pause that thread, but since
"Thread.Suspend" is being deprecated and since "Thread.Sleep" is for the
current thread -not another as i need- then... how I can pause it???
Thanks,

Néstor Sánchez A.
AlexS - 27 Jun 2007 20:27 GMT
Can you pass pauseFlag to the thread through some object? Alternatively, if
thread is encapsulated in some object (say, Job), can you add Pause event,
which you can invoke from controlling thread?

You can use Sleep on any thread - it blocks current thread. See
Thread.CurrentThread.

HTH
Alex

> Hi,
> i'm writing a windows service for wich a need to run an auxiliary thread.
[quoted text clipped - 4 lines]
>
> Néstor Sánchez A.
Néstor Sánchez A. - 28 Jun 2007 23:32 GMT
That's the problem... Thread.Sleep is for pause the current thread and my
need is to pause another thread, let say one running something outside my
development scope (classes on a library).
For .NET 1.1 there was Thread.Suspend and Thread.Resume. But these were
dangerous in some scenarios and were marked as obsolete and will be
deprecated in next versions of the framework as stated by documentation but
there is no alternative?

NSA

> Can you pass pauseFlag to the thread through some object? Alternatively,
> if thread is encapsulated in some object (say, Job), can you add Pause
[quoted text clipped - 14 lines]
>>
>> Néstor Sánchez A.
AlexS - 29 Jun 2007 21:34 GMT
That's why I asked if you can pass stop flag to another thread. Then you can
Sleep on this flag.

If you can't change coding for threads to pause, can you just kill the
thread and restart it when required? Is there's some stop flag / event
available. If not, probably you need to re-consider how threads were
designed initially. In worst case you can put them into another process and
kill processes hoping that Windows will properly cleanup killed process.
However in this case you might need to deal with possible resource leaks.

Lacking other details (how you start threads, which parameters are
available, or if it's possible to pass some dummy job to do nothing to
emulate sleep) it's not easy to propose something more sensible.

> That's the problem... Thread.Sleep is for pause the current thread and my
> need is to pause another thread, let say one running something outside my
[quoted text clipped - 25 lines]
>>>
>>> Néstor Sánchez A.
Peter Ritchie [C# MVP] - 10 Jul 2007 19:20 GMT
Thread.Sleep is never the correct means to pause a thread.  What does "pause"
mean?  For x milliseconds?  Thread.Sleep does not do that; it sleeps for a
minimum of x milliseconds it's almost impossible for your thread to Sleep for
the number of milliseconds passed to Thread.Sleep, and the granularity
depends on the version of Windows running (could range in granularity of
15-25 ms).  Plus, you've guaranteed your thread to be busy for x
milliseconds, which means the rest of your application must wait for this
thread to finish sleeping before it exits (unless you don't mind aborting the
thread; which is a bad thing).

If by "pause" you mean "pause until I ask you to awake" (which is usually
want you want to do) then Thread.Sleep is total inappropriate.

See  
http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-o
f-a-poorly-designed-program.aspx

for more detail on why Thread.Sleep is a sign of a poorly designed program.  
And see
http://msmvps.com/blogs/peterritchie/archive/2006/10/13/_2700_System.Threading.T
hread.Suspend_280029002700_-is-obsolete_3A00_-_2700_Thread.Suspend-has-been-depr
ecated_2E002E002E00_.aspx

for one means of writing a thread that can be asked to pause, resume and
terminate.

Signature

Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#

> That's why I asked if you can pass stop flag to another thread. Then you can
> Sleep on this flag.
[quoted text clipped - 36 lines]
> >>> is for the current thread -not another as i need- then... how I can
> >>> pause it???
Néstor Sánchez A. - 13 Jul 2007 00:53 GMT
Thank you so much. I'll take the second link solution.

NSA

> Thread.Sleep is never the correct means to pause a thread.  What does
> "pause"
[quoted text clipped - 67 lines]
>> >>> is for the current thread -not another as i need- then... how I can
>> >>> pause it???

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.