Threading question Sorry of this is repost

P

Phillip N Rounds

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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Phillip N Rounds said:
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
 
S

Sean Hederman

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 said:
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
 
W

Willy Denoyette [MVP]

Phillip N Rounds said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top