Threading question

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
 
W

Willy Denoyette [MVP]

And what's the value of N
in
for ( int i =0; i < N; i++)

Willy.

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
 
N

Nick Malik [Microsoft]

gotta love this line in the Main:
while ( true )

Basically, this is an infinite loop.

Try this:
Thread.Sleep(Timeout.Infinite);

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
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]

Nick Malik said:
gotta love this line in the Main:
while ( true )

Basically, this is an infinite loop.

Try this:
Thread.Sleep(Timeout.Infinite);

--

Missed that one, this alone must take ~100% CPU time.

Willy.
 
A

Alvin Bruney [MVP]

starting to see chinks in the armor willy.

--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
 

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