Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Thread taking 100% CPU

Reply
 
Old 11-06-2008   #1 (permalink)
Michel Racicot


 
 

Thread taking 100% CPU

In my application, I have a thread that takes 100% of the CPU (Infinite
Loop)

What is the best way to prevent that? adding in a thread.sleep??? in the
While loop?

The code is the following:

// NetListener is a TCPListener

private void ListeningLoop()
{
while (Listening)
{
if (NetListener.Pending())
{
TcpClient ConnectedClient = NetListener.AcceptTcpClient();
if (ConnectedClient.Connected)
{
CurrentServer.AddClient(ConnectedClient);
_Logger.Log("New Client - " +
ConnectedClient.Client.RemoteEndPoint.ToString());
}
}
}
}



My System SpecsSystem Spec
Old 11-06-2008   #2 (permalink)
Dick Grier


 
 

Re: Thread taking 100% CPU

Try:

Thread.Sleep(1);

inside the loop.

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.


My System SpecsSystem Spec
Old 11-06-2008   #3 (permalink)
Michel Racicot


 
 

Re: Thread taking 100% CPU

It's working, but shouldn't the sleep parameter be greater than 1 to
minimize even more the time taken?

Also, is there another solution or is it the definitive best one?

Thank you btw


"Dick Grier" <dick_grierNOSPAM@xxxxxx> wrote in message
news:ezUVPcDQJHA.3876@xxxxxx
Quote:

> Try:
>
> Thread.Sleep(1);
>
> inside the loop.
>
> --
> Richard Grier, MVP
> Hard & Software
> Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
> Edition,
> ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
> 2006.
> See www.hardandsoftware.net for details and contact information.
>

My System SpecsSystem Spec
Old 11-06-2008   #4 (permalink)
Phil Wilson


 
 

Re: Thread taking 100% CPU

It's up to you - how important is it to respond quickly? If once a second is
ok, sleep for a second.

--
Phil Wilson
Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972


"Michel Racicot" <mracicot@xxxxxx> wrote in message
news:uDgSs4DQJHA.4424@xxxxxx
Quote:

> It's working, but shouldn't the sleep parameter be greater than 1 to
> minimize even more the time taken?
>
> Also, is there another solution or is it the definitive best one?
>
> Thank you btw
>
>
> "Dick Grier" <dick_grierNOSPAM@xxxxxx> wrote in message
> news:ezUVPcDQJHA.3876@xxxxxx
Quote:

>> Try:
>>
>> Thread.Sleep(1);
>>
>> inside the loop.
>>
>> --
>> Richard Grier, MVP
>> Hard & Software
>> Author of Visual Basic Programmer's Guide to Serial Communications,
>> Fourth Edition,
>> ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
>> 2006.
>> See www.hardandsoftware.net for details and contact information.
>>
>
>

My System SpecsSystem Spec
Old 11-06-2008   #5 (permalink)
Stanimir Stoyanov


 
 

Re: Thread taking 100% CPU

Do you really have to continue adding clients ad infinitum? If not, you
should consider changing the way you handle connections. E.g. only add a
client when the existing connection drops, which would not use the CPU at
its maximum.
--
Stanimir Stoyanov
http://stoyanoff.info

"Michel Racicot" <mracicot@xxxxxx> wrote in message
news:et2fL%23BQJHA.1172@xxxxxx
Quote:

> In my application, I have a thread that takes 100% of the CPU (Infinite
> Loop)
>
> What is the best way to prevent that? adding in a thread.sleep??? in the
> While loop?
>
> The code is the following:
>
> // NetListener is a TCPListener
>
> private void ListeningLoop()
> {
> while (Listening)
> {
> if (NetListener.Pending())
> {
> TcpClient ConnectedClient = NetListener.AcceptTcpClient();
> if (ConnectedClient.Connected)
> {
> CurrentServer.AddClient(ConnectedClient);
> _Logger.Log("New Client - " +
> ConnectedClient.Client.RemoteEndPoint.ToString());
> }
> }
> }
> }
>
My System SpecsSystem Spec
Old 11-07-2008   #6 (permalink)
Dick Grier


 
 

Re: Thread taking 100% CPU

Sure. Experiment.

In practice (I suspect), a number greater than 10 will not result in any
perceived improvement, and will decrease the response time of you
application to a request.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How do the current thread get thread notification of OS intruption .NET General
Start a new thread from an existing thread, which was started from atimer .NET General
WmdHost.exe TAKING ALL RAM available, locking system, and taking f Vista hardware & devices
Unblock not taking... Vista security
Taking Ownership? Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46