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
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