Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
tools
vsnet.act
vsnet.debugging
vsnet.documentation
vsnet.enterprise.tools
vsnet.general
vsnet.ide
vsnet.jlca
vsnet.servicepacks
vsnet.setup
vsnet.vsip
vsnet.vss
vsnet.vstools.office
vstudio.development
vstudio.extensibility
vstudio.general
vstudio.helpauthoring
vstudio.setup
vstudio.sourcesafe
  
 
date: Thu, 14 Aug 2008 14:31:32 +0100,    group: microsoft.public.vstudio.development        back       


Suspending threads   
Hi,

If I create a thread in the non-suspended state and then call:

SuspendThread(hThread);

how do I know that the thread isn't already suspended when I call this?
Ideally, I'd like something like this:

if (GetThreadSuspendCount(hThread) == 0)
    SuspendThread(hThread);

but the MSDN documentation is a little unhelpful here.

Thanks,
Alain
date: Thu, 14 Aug 2008 14:31:32 +0100   author:   Alain Dekker

Re: Suspending threads   
On Thu, 14 Aug 2008 14:31:32 +0100, "Alain Dekker"
 wrote:

>Hi,
>
>If I create a thread in the non-suspended state and then call:
>
>SuspendThread(hThread);
>
>how do I know that the thread isn't already suspended when I call this?
>Ideally, I'd like something like this:
>
>if (GetThreadSuspendCount(hThread) == 0)
>    SuspendThread(hThread);
>
>but the MSDN documentation is a little unhelpful here.
>
>Thanks,
>Alain 
>

If the function succeeds, the return value is the thread's previous suspend
count; otherwise, it is 0xFFFFFFFF. To get extended error information, use
the GetLastError function. 


if (SuspendThread(hThread) > 0)
    ResumeThread(hThread);
date: Thu, 14 Aug 2008 08:33:16 -0700   author:   Geoff lid

Re: Suspending threads   
"Alain Dekker"  wrote in message 
news:OQYF0Gh$IHA.4648@TK2MSFTNGP04.phx.gbl...
> Hi,
>
> If I create a thread in the non-suspended state and then call:
>
> SuspendThread(hThread);
>
> how do I know that the thread isn't already suspended when I call this?
> Ideally, I'd like something like this:
>
> if (GetThreadSuspendCount(hThread) == 0)
>    SuspendThread(hThread);
>
> but the MSDN documentation is a little unhelpful here.


Actually the MSDN docs describe it completely.

Beware of using SuspendThread() unless you're writing a debugger.
You have NO control over where (in the execution) the thread gets 
suspended...this can cause problems like deadlocks and data corruption.

Mark

-- 
Mark Salsbery
Microsoft MVP - Visual C++


>
> Thanks,
> Alain
>
date: Thu, 14 Aug 2008 09:34:34 -0700   author:   Mark Salsbery [MVP] MarkSalsbery[MVP]@newsgroup.nospam

Re: Suspending threads   
Hi Mark,

If the MSDN documentation describes it completely, therefore the answer 
(which is not described, but surely I should have inferred this) is that I 
cannot get the SuspendCount of the thread before I call SuspendThread(...)! 
Thats a pity.

Thanks for the answer. What I'll do is to track the return from 
SuspendThread to ensure it doesn't get called twice.

Is there any way to suspend a thread in a known state, by for example, 
getting it to restart at a certain point in the execution? I have an 
application that uses threads to communicate with COM ports, read processor 
load using WMI, communicate across the network for reporting and so on. If 
one or more of those threads is not required, I'd like to suspend it to save 
processing power since its a pretty high-speed app and the main thread needs 
all the processor time it can get (without completely starving the other 
threads!).

Got any suggestions or things I should considerations for this type of 
"Suspend-Resume-Suspend-Resume" chain?

Thanks and regards,
Alain


"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospam> wrote in message 
news:u4C3pui$IHA.4616@TK2MSFTNGP06.phx.gbl...
> "Alain Dekker"  wrote in message 
> news:OQYF0Gh$IHA.4648@TK2MSFTNGP04.phx.gbl...
>> Hi,
>>
>> If I create a thread in the non-suspended state and then call:
>>
>> SuspendThread(hThread);
>>
>> how do I know that the thread isn't already suspended when I call this?
>> Ideally, I'd like something like this:
>>
>> if (GetThreadSuspendCount(hThread) == 0)
>>    SuspendThread(hThread);
>>
>> but the MSDN documentation is a little unhelpful here.
>
>
> Actually the MSDN docs describe it completely.
>
> Beware of using SuspendThread() unless you're writing a debugger.
> You have NO control over where (in the execution) the thread gets 
> suspended...this can cause problems like deadlocks and data corruption.
>
> Mark
>
> -- 
> Mark Salsbery
> Microsoft MVP - Visual C++
>
>
>>
>> Thanks,
>> Alain
>>
date: Fri, 15 Aug 2008 07:20:29 +0100   author:   Alain Dekker

Re: Suspending threads   
"Alain Dekker"  wrote in message 
news:uSyUp7p$IHA.3964@TK2MSFTNGP06.phx.gbl...
> Hi Mark,
>
> If the MSDN documentation describes it completely, therefore the answer 
> (which is not described, but surely I should have inferred this) is that I 
> cannot get the SuspendCount of the thread before I call 
> SuspendThread(...)! Thats a pity.
>
> Thanks for the answer. What I'll do is to track the return from 
> SuspendThread to ensure it doesn't get called twice.
>
> Is there any way to suspend a thread in a known state, by for example, 
> getting it to restart at a certain point in the execution? I have an 
> application that uses threads to communicate with COM ports, read 
> processor load using WMI, communicate across the network for reporting and 
> so on. If one or more of those threads is not required, I'd like to 
> suspend it to save processing power since its a pretty high-speed app and 
> the main thread needs all the processor time it can get (without 
> completely starving the other threads!).
>
> Got any suggestions or things I should considerations for this type of 
> "Suspend-Resume-Suspend-Resume" chain?


I typically use event synchronization objects.

Here's an example (pseudocode) using two events to wake a thread - one to 
tell the thread to terminate and one to signal the thread to do some work. 
The events can be signaled by any other thread...

threadproc
{
   while (1)
   {
      // block until an event is signaled
      waitformultipleobjects(terminateevent, dosomeworkevent);

      if (terminateevent was signaled)  break;

       ... do some work then loop and wait for next event ...
   }
}


Mark

-- 
Mark Salsbery
Microsoft MVP - Visual C++



>
> Thanks and regards,
> Alain
date: Fri, 15 Aug 2008 11:37:50 -0700   author:   Mark Salsbery [MVP] MarkSalsbery[MVP]@newsgroup.nospam

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us