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

Threading question Sorry of this is repost

Why is this using 50% of available CPU?

What I am trying to accomplish in this service is as follows: In the
main class (COS_Service) , int Main(), initializes a timer. Each OnTimer()
event is to create multiple threads to perform their assigned task. I also
want to do some checking on the results of the prior OnTimer() event.

Currently, this process consumes ~50% of availble CPU time to do (
virtually) nothing. Actually, I have dummied out all processing in the
worker thread, and it still consumes all the CPU.

So, the pseudo code is this:

public class COS_Service : System.ServiceProcess.ServiceBase
{
public static System.Collections.ArrayList workers;
public static System.Timers.Timer MainTimer;
static void Main()
{
workers = new ArrayList();
MainTimer = new Timer();
MainTimer.Elapsed+= new ElapsedEventHandler( OnTimer );
MainTimer.Interval = 1000;
MainTimer.Enabled = true;
while ( true )
}
public static void OnTimer( object Source, ElapsedEventArgs e)
{
while ( workers.Count > 0 ) // This
is where I handle processing of the threads created in the prior call.
{
switch ( ((MainWorkerClass) workers[0]).iStatusOfThisClass )
// I Process on the results
{
case -1: // Default
HandleThis(); break;
case 0: // Failed
HandleFailure(); break;
case 1: // Success
HandleSuccess(); break;
}
workers.RemoveAt(0);
// And I delete the object
}

for ( int i =0; i < N; i++)
// Here I create a batch of new worker threads
{
MainWorkerClass mwc = new MainWorkerClass();
// Create an instance of the class
mwc.InfoINeedForThisStuff = i.ToString();
// Initialize some variables
Thread thr_mwc = new Thread( new ThreadStart(
mwc.InitiateProcess() ); // Create the thread to do the work
workers.Acc( mwc );
// store the instance of the worker class
thr_MWC.Start();
// Start it working
}
}
}

internal class MainWorker // This is the worker class
which does all of the threaded processing.
{
public int iStatusOfThisClass = -1;
public string InfoINeedForThisStuff;
public void InitiateProcess() // This is the
function which is to be the callback function for instantiating the thread
{
if ( DoSomeStuff( InfoINeedForThisStuff ).IsGood)
iStatusOfThisClass = 1;
else {iStatusOfThisClass = 0;
Thread.CurrentThread.Abort(); return; }
try
{ DoSomeOtherStuff() ;
if ( ThisResult == Bad ) { iStatusOfThisClass =0;
Thread.CurrentThread.Abort(); return; }
}
catch ( MyError e ) { iStatusOfThisClass =0;
Thread.CurrentThread.Abort(); return; }
Thread.CurrentThread.Abort();
return;
}
}

Any input is appreciated.

Phil

Nov 16 '05 #1
3 1835
Phillip,

I am curious, why are you creating your own pool of threads? I think
you will see better utilization if you use the ThreadPool. Creating and
tearing down all of those threads is going to have some overhead, which is
overcome (to some degree) by using the thread pool.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Phillip N Rounds" <pr*****@cassandragroup.com> wrote in message
news:uC*************@TK2MSFTNGP15.phx.gbl...
Why is this using 50% of available CPU?

What I am trying to accomplish in this service is as follows: In the
main class (COS_Service) , int Main(), initializes a timer. Each
OnTimer()
event is to create multiple threads to perform their assigned task. I
also
want to do some checking on the results of the prior OnTimer() event.

Currently, this process consumes ~50% of availble CPU time to do (
virtually) nothing. Actually, I have dummied out all processing in the
worker thread, and it still consumes all the CPU.

So, the pseudo code is this:

public class COS_Service : System.ServiceProcess.ServiceBase
{
public static System.Collections.ArrayList workers;
public static System.Timers.Timer MainTimer;
static void Main()
{
workers = new ArrayList();
MainTimer = new Timer();
MainTimer.Elapsed+= new ElapsedEventHandler( OnTimer );
MainTimer.Interval = 1000;
MainTimer.Enabled = true;
while ( true )
}
public static void OnTimer( object Source, ElapsedEventArgs e)
{
while ( workers.Count > 0 ) // This
is where I handle processing of the threads created in the prior call.
{
switch ( ((MainWorkerClass) workers[0]).iStatusOfThisClass )
// I Process on the results
{
case -1: // Default
HandleThis(); break;
case 0: // Failed
HandleFailure(); break;
case 1: // Success
HandleSuccess(); break;
}
workers.RemoveAt(0);
// And I delete the object
}

for ( int i =0; i < N; i++)
// Here I create a batch of new worker threads
{
MainWorkerClass mwc = new MainWorkerClass();
// Create an instance of the class
mwc.InfoINeedForThisStuff = i.ToString();
// Initialize some variables
Thread thr_mwc = new Thread( new ThreadStart(
mwc.InitiateProcess() ); // Create the thread to do the work
workers.Acc( mwc );
// store the instance of the worker class
thr_MWC.Start();
// Start it working
}
}
}

internal class MainWorker // This is the worker class
which does all of the threaded processing.
{
public int iStatusOfThisClass = -1;
public string InfoINeedForThisStuff;
public void InitiateProcess() // This is the
function which is to be the callback function for instantiating the thread
{
if ( DoSomeStuff( InfoINeedForThisStuff ).IsGood)
iStatusOfThisClass = 1;
else {iStatusOfThisClass = 0;
Thread.CurrentThread.Abort(); return; }
try
{ DoSomeOtherStuff() ;
if ( ThisResult == Bad ) { iStatusOfThisClass =0;
Thread.CurrentThread.Abort(); return; }
}
catch ( MyError e ) { iStatusOfThisClass =0;
Thread.CurrentThread.Abort(); return; }
Thread.CurrentThread.Abort();
return;
}
}

Any input is appreciated.

Phil

Nov 16 '05 #2
A big culprit would be that while(true) you have in there.

How about:
Thread.Sleep(Timeout.Infinite);

Of course that would mean that the program will NEVER complete, rather use a
service then.

"Phillip N Rounds" <pr*****@cassandragroup.com> wrote in message
news:uC*************@TK2MSFTNGP15.phx.gbl...
Why is this using 50% of available CPU?

What I am trying to accomplish in this service is as follows: In the
main class (COS_Service) , int Main(), initializes a timer. Each
OnTimer()
event is to create multiple threads to perform their assigned task. I
also
want to do some checking on the results of the prior OnTimer() event.

Currently, this process consumes ~50% of availble CPU time to do (
virtually) nothing. Actually, I have dummied out all processing in the
worker thread, and it still consumes all the CPU.

So, the pseudo code is this:

public class COS_Service : System.ServiceProcess.ServiceBase
{
public static System.Collections.ArrayList workers;
public static System.Timers.Timer MainTimer;
static void Main()
{
workers = new ArrayList();
MainTimer = new Timer();
MainTimer.Elapsed+= new ElapsedEventHandler( OnTimer );
MainTimer.Interval = 1000;
MainTimer.Enabled = true;
while ( true )
}
public static void OnTimer( object Source, ElapsedEventArgs e)
{
while ( workers.Count > 0 ) // This
is where I handle processing of the threads created in the prior call.
{
switch ( ((MainWorkerClass) workers[0]).iStatusOfThisClass )
// I Process on the results
{
case -1: // Default
HandleThis(); break;
case 0: // Failed
HandleFailure(); break;
case 1: // Success
HandleSuccess(); break;
}
workers.RemoveAt(0);
// And I delete the object
}

for ( int i =0; i < N; i++)
// Here I create a batch of new worker threads
{
MainWorkerClass mwc = new MainWorkerClass();
// Create an instance of the class
mwc.InfoINeedForThisStuff = i.ToString();
// Initialize some variables
Thread thr_mwc = new Thread( new ThreadStart(
mwc.InitiateProcess() ); // Create the thread to do the work
workers.Acc( mwc );
// store the instance of the worker class
thr_MWC.Start();
// Start it working
}
}
}

internal class MainWorker // This is the worker class
which does all of the threaded processing.
{
public int iStatusOfThisClass = -1;
public string InfoINeedForThisStuff;
public void InitiateProcess() // This is the
function which is to be the callback function for instantiating the thread
{
if ( DoSomeStuff( InfoINeedForThisStuff ).IsGood)
iStatusOfThisClass = 1;
else {iStatusOfThisClass = 0;
Thread.CurrentThread.Abort(); return; }
try
{ DoSomeOtherStuff() ;
if ( ThisResult == Bad ) { iStatusOfThisClass =0;
Thread.CurrentThread.Abort(); return; }
}
catch ( MyError e ) { iStatusOfThisClass =0;
Thread.CurrentThread.Abort(); return; }
Thread.CurrentThread.Abort();
return;
}
}

Any input is appreciated.

Phil

Nov 16 '05 #3

"Phillip N Rounds" <pr*****@cassandragroup.com> wrote in message
news:uC*************@TK2MSFTNGP15.phx.gbl...
Why is this using 50% of available CPU?
*** Following is burning all available CPU cycles (50%, guess you are
running on an HT CPU) !! while ( true )
}
*** What's the value of N?
for ( int i =0; i < N; i++)
// Here I create a batch of new worker threads
{

Not sure why you repost, did you look at your other post's replies?

Willy.
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jon Sequeira | last post by:
I'm have a class that represents shipping cost data for a commerce web site. The underlying data may only change once a month, maybe less, so I'd rather not hit the database every time the class is...
16
by: One Handed Man \( OHM - Terry Burns \) | last post by:
Sorry if this gets duplicated, but I posted it and cant see it for a long time so repost. . . I have an application which is writing to a graphics object, the main UI thread and a worker thread...
4
by: Bob | last post by:
- For cleanup, is it sufficient to set a Thread to Nothing after it's done? - It is OK to pass objects out of the thread? (dumb question maybe but I want to be sure) - What's the best way to...
2
by: Jon Sequeira | last post by:
I'm have a class that represents shipping cost data for a commerce web site. The underlying data may only change once a month, maybe less, so I'd rather not hit the database every time the class is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.