473,503 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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());
}
}
}
}
Nov 6 '08 #1
5 3529
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.
Nov 6 '08 #2
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@.msn.comwrote in message
news:ez**************@TK2MSFTNGP04.phx.gbl...
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.

Nov 6 '08 #3
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" <mr******@hotmail.comwrote in message
news:uD**************@TK2MSFTNGP06.phx.gbl...
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@.msn.comwrote in message
news:ez**************@TK2MSFTNGP04.phx.gbl...
>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.


Nov 6 '08 #4
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" <mr******@hotmail.comwrote in message
news:et****************@TK2MSFTNGP03.phx.gbl...
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());
}
}
}
}
Nov 6 '08 #5
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.
Nov 7 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10448
by: Kranthis | last post by:
Hi, I want to restart a thread after same variable value in the thread reaches 100 in c# . How can I do. Please suggest me any solution
7
5927
by: utkarsh | last post by:
Hi, I am using the following code to the update a section of the image which is drawn on a panel. this.m_Timer = new System.Windows.Forms.Timer(this.components); this.m_Timer.Tick += new...
3
1447
by: Phil | last post by:
Hi, I've a simple page with one button. In the OnClick event, I want to create a thread that will perform a long runninf task. Here is a sample code in the OnClick Thread t = new Thread(new...
5
5568
by: archana | last post by:
Hi all i am having application which is using asychronous web request. At a time i am processing 5 urls asynchronously. Application working properly for 5 asynchronous call. But sometimes CPU...
5
1624
by: fniles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message...
3
6152
by: Raj Wall | last post by:
Hi, I have an application that uses a number of sub-threads. What is the best way to do some processing in each thread when the main application is shut down? Is the ThreadAbortException...
2
1949
by: Don | last post by:
How to stop a process which is running in a separate thread!!! I've got a class which performs some lengthy process in a background (separate) thread. And this lengthy process raises events...
0
2665
by: Buckaroo Banzai | last post by:
Hello, newbie here... I'm writing this program but when I click the start button which should initiate either the Hare or the Tortoise, it does not, this is the first time I use threads, so the...
2
2072
by: warjie | last post by:
Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on. what's wrong with this? i want to display the thread result to a text box....
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
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...
0
7278
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,...
1
6988
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...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
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 ...

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.