473,856 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ServiceP rocess.ServiceB ase
{
public static System.Collecti ons.ArrayList workers;
public static System.Timers.T imer MainTimer;
static void Main()
{
workers = new ArrayList();
MainTimer = new Timer();
MainTimer.Elaps ed+= new ElapsedEventHan dler( OnTimer );
MainTimer.Inter val = 1000;
MainTimer.Enabl ed = true;
while ( true )
}
public static void OnTimer( object Source, ElapsedEventArg s e)
{
while ( workers.Count > 0 ) // This
is where I handle processing of the threads created in the prior call.
{
switch ( ((MainWorkerCla ss) workers[0]).iStatusOfThis Class )
// I Process on the results
{
case -1: // Default
HandleThis(); break;
case 0: // Failed
HandleFailure() ; break;
case 1: // Success
HandleSuccess() ; break;
}
workers.RemoveA t(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.InfoINeedFo rThisStuff = i.ToString();
// Initialize some variables
Thread thr_mwc = new Thread( new ThreadStart(
mwc.InitiatePro cess() ); // 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 iStatusOfThisCl ass = -1;
public string InfoINeedForThi sStuff;
public void InitiateProcess () // This is the
function which is to be the callback function for instantiating the thread
{
if ( DoSomeStuff( InfoINeedForThi sStuff ).IsGood)
iStatusOfThisCl ass = 1;
else {iStatusOfThisC lass = 0;
Thread.CurrentT hread.Abort(); return; }
try
{ DoSomeOtherStuf f() ;
if ( ThisResult == Bad ) { iStatusOfThisCl ass =0;
Thread.CurrentT hread.Abort(); return; }
}
catch ( MyError e ) { iStatusOfThisCl ass =0;
Thread.CurrentT hread.Abort(); return; }
Thread.CurrentT hread.Abort();
return;
}
}

Any input is appreciated.

Phil

Nov 16 '05 #1
3 1857
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.co m

"Phillip N Rounds" <pr*****@cassan dragroup.com> wrote in message
news:uC******** *****@TK2MSFTNG P15.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.ServiceP rocess.ServiceB ase
{
public static System.Collecti ons.ArrayList workers;
public static System.Timers.T imer MainTimer;
static void Main()
{
workers = new ArrayList();
MainTimer = new Timer();
MainTimer.Elaps ed+= new ElapsedEventHan dler( OnTimer );
MainTimer.Inter val = 1000;
MainTimer.Enabl ed = true;
while ( true )
}
public static void OnTimer( object Source, ElapsedEventArg s e)
{
while ( workers.Count > 0 ) // This
is where I handle processing of the threads created in the prior call.
{
switch ( ((MainWorkerCla ss) workers[0]).iStatusOfThis Class )
// I Process on the results
{
case -1: // Default
HandleThis(); break;
case 0: // Failed
HandleFailure() ; break;
case 1: // Success
HandleSuccess() ; break;
}
workers.RemoveA t(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.InfoINeedFo rThisStuff = i.ToString();
// Initialize some variables
Thread thr_mwc = new Thread( new ThreadStart(
mwc.InitiatePro cess() ); // 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 iStatusOfThisCl ass = -1;
public string InfoINeedForThi sStuff;
public void InitiateProcess () // This is the
function which is to be the callback function for instantiating the thread
{
if ( DoSomeStuff( InfoINeedForThi sStuff ).IsGood)
iStatusOfThisCl ass = 1;
else {iStatusOfThisC lass = 0;
Thread.CurrentT hread.Abort(); return; }
try
{ DoSomeOtherStuf f() ;
if ( ThisResult == Bad ) { iStatusOfThisCl ass =0;
Thread.CurrentT hread.Abort(); return; }
}
catch ( MyError e ) { iStatusOfThisCl ass =0;
Thread.CurrentT hread.Abort(); return; }
Thread.CurrentT hread.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(Ti meout.Infinite) ;

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

"Phillip N Rounds" <pr*****@cassan dragroup.com> wrote in message
news:uC******** *****@TK2MSFTNG P15.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.ServiceP rocess.ServiceB ase
{
public static System.Collecti ons.ArrayList workers;
public static System.Timers.T imer MainTimer;
static void Main()
{
workers = new ArrayList();
MainTimer = new Timer();
MainTimer.Elaps ed+= new ElapsedEventHan dler( OnTimer );
MainTimer.Inter val = 1000;
MainTimer.Enabl ed = true;
while ( true )
}
public static void OnTimer( object Source, ElapsedEventArg s e)
{
while ( workers.Count > 0 ) // This
is where I handle processing of the threads created in the prior call.
{
switch ( ((MainWorkerCla ss) workers[0]).iStatusOfThis Class )
// I Process on the results
{
case -1: // Default
HandleThis(); break;
case 0: // Failed
HandleFailure() ; break;
case 1: // Success
HandleSuccess() ; break;
}
workers.RemoveA t(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.InfoINeedFo rThisStuff = i.ToString();
// Initialize some variables
Thread thr_mwc = new Thread( new ThreadStart(
mwc.InitiatePro cess() ); // 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 iStatusOfThisCl ass = -1;
public string InfoINeedForThi sStuff;
public void InitiateProcess () // This is the
function which is to be the callback function for instantiating the thread
{
if ( DoSomeStuff( InfoINeedForThi sStuff ).IsGood)
iStatusOfThisCl ass = 1;
else {iStatusOfThisC lass = 0;
Thread.CurrentT hread.Abort(); return; }
try
{ DoSomeOtherStuf f() ;
if ( ThisResult == Bad ) { iStatusOfThisCl ass =0;
Thread.CurrentT hread.Abort(); return; }
}
catch ( MyError e ) { iStatusOfThisCl ass =0;
Thread.CurrentT hread.Abort(); return; }
Thread.CurrentT hread.Abort();
return;
}
}

Any input is appreciated.

Phil

Nov 16 '05 #3

"Phillip N Rounds" <pr*****@cassan dragroup.com> wrote in message
news:uC******** *****@TK2MSFTNG P15.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
1617
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 needed. My concerns are these: I want it to be thread-safe for any number of simultaneous sessions. I want it to be efficient-- is making one object available to potentially hundreds of simultaneous sessions an inefficient approach?
16
351
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 are writing to the same object, whats the best way to manage this ? Thanks OHM --
4
1312
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 process messages coming out of a thread? I want to queue them up, but MessageQueue doesn't look like what I need. Should I just make my own queue class? If so I'll have to worry about enumerator synchronization... a pointer to a 'best practice'...
2
278
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 needed. My concerns are these: I want it to be thread-safe for any number of simultaneous sessions. I want it to be efficient-- is making one object available to potentially hundreds of simultaneous sessions an inefficient approach?
0
9762
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11056
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10696
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10782
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5761
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4575
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4174
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3201
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.