![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||