[dba-VB] Thread.sleep

jwcolby jwcolby at colbyconsulting.com
Wed Apr 4 19:51:03 CDT 2012


 >> Care to share the code that worked for you?

LOL.  Sure.  I am finishing up troubleshooting / debugging my RunState class.  I had this class 
encapsulating a thread and delegates as well as delegates for sinking and handling check boxes and 
labels.  The objective was to have an encapsulated class which handled a check box or button which 
the user (me) could check to start a process / uncheck to stop a process.  That was working just 
fine, the thread would run whatever process code needed to run in a while loop -

While (RunState - runState.Started)
{
	Do whatever needed doing
	thread.sleep(pSleepTime)
}

It was during this thread.Sleep() that the processor usage would spike.  I had 4 cores in my dev 
machine (running this code) and all 4 would get busy, the aggregate would be about 25% of the total 
CPU power.

Really strange!

I decided that this really would be better handled with a timer class so now I set up a timer and do 
something like:

if (RunState - runState.Started)
{
	Do whatever needed doing
}

No more thread.sleep() and no more CPU usage spike.  I am integrating that into my RunState class so 
that I (the developer) can decide whether the RunState should just run code on the calling thread 
(no thread object), start a thread object based in clsRunState, or start a timer in clsRunState.

It is coming back up and I have rewritten my code in two places (so far) which used that

while ()
{
	dosomething;
	thread.sleep(pSleepTime);
}

construct to now use a timer.  The timer.Interval is the same value as the pSleepTime, IOW instead 
of sleeping a thread for 10 seconds I fire a timer every 10 seconds.  Basically I am just doing 
things like checking whether I have a supervisor record ready to process, whether I have any new 
supervisor records etc.

The timer is much cleaner (kinda) and one real upside is that the system is not waiting for the 
sleep to finish.

Anyway, I will post the clsRunstate code I suppose if people promise not to laugh.  I am still very 
much a junior C# programmer.

John W. Colby
Colby Consulting

Reality is what refuses to go away
when you do not believe in it

On 4/4/2012 6:16 PM, David McAfee wrote:
> Did my message ever come through?
>
> On Wed, Apr 4, 2012 at 12:17 PM, David McAfee<davidmcafee at gmail.com>  wrote:
>
>> Garbage Collection, although I think you are being facetious. :)
>>
>>
>> You have stirred up in my head regarding a project that I have out there
>> that seems to cause memory issues on certain computers.
>>
>> I haven't been able to recreate this in my office.
>>
>> I do use sleep as well in several places in that app.
>>
>> Care to share the code that worked for you?
>>
>> Thanks,
>> David
>>
> _______________________________________________
> dba-VB mailing list
> dba-VB at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-vb
> http://www.databaseadvisors.com
>
>



More information about the dba-VB mailing list