Windows Service Comsuming 99% CPU
-
Hello, this is Vikash Gohil. I have Created a Windows Service which does the following when service is started : Connects SQL Server 2000 Database. Starts a Threads. Thread executes a while loop In the while loop, Current system time is taken using : format(now,"HH:mm") A Query is made to a Table in Database to look for records as below. "Select From Where ='" & format(now,"HH:mm") & "'" if any record exists, then a particular procedure gets executed. The above code continues to run in a while loop 24/7 The problem I am facing is, the service takes aroung 98% CPU Usage. The Database Table contains only 20 to 25 records. I want to know why this thing is happening. Is it bcoz of the while loop. If yes, then what to use instead of while loop? I Can't change the Service functionality as it is a client requirement. Please can anyone guide me in this matter. Any help would be greatly appreciated. Thanks in Advance, awaiting a reply soon.
-
Hello, this is Vikash Gohil. I have Created a Windows Service which does the following when service is started : Connects SQL Server 2000 Database. Starts a Threads. Thread executes a while loop In the while loop, Current system time is taken using : format(now,"HH:mm") A Query is made to a Table in Database to look for records as below. "Select From Where ='" & format(now,"HH:mm") & "'" if any record exists, then a particular procedure gets executed. The above code continues to run in a while loop 24/7 The problem I am facing is, the service takes aroung 98% CPU Usage. The Database Table contains only 20 to 25 records. I want to know why this thing is happening. Is it bcoz of the while loop. If yes, then what to use instead of while loop? I Can't change the Service functionality as it is a client requirement. Please can anyone guide me in this matter. Any help would be greatly appreciated. Thanks in Advance, awaiting a reply soon.
-
Consider using
System.Threading.Thread.Sleep( int milliseconds )
. That should unburden the processor. CheersI don't like my signature at all
Hello, Estys. Thanks for your reply. I thought of using Sleep. But many posts on the net suggest that using Sleep is a bad practice. So is there any risks or disadvantages in using Sleep. Please reply if you know anything about this. Thanks Again.
-
Hello, Estys. Thanks for your reply. I thought of using Sleep. But many posts on the net suggest that using Sleep is a bad practice. So is there any risks or disadvantages in using Sleep. Please reply if you know anything about this. Thanks Again.
VikashGohil wrote:
But many posts on the net suggest that using Sleep is a bad practice.
Many things are 'Bad Practice' but they still get done. Have you never used a GOTO? :)
------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
-
VikashGohil wrote:
But many posts on the net suggest that using Sleep is a bad practice.
Many things are 'Bad Practice' but they still get done. Have you never used a GOTO? :)
------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
Hello, Dalek. Thanks for Reply. I will surely try using Sleep. Thanks.
-
Hello, Estys. Thanks for your reply. I thought of using Sleep. But many posts on the net suggest that using Sleep is a bad practice. So is there any risks or disadvantages in using Sleep. Please reply if you know anything about this. Thanks Again.
Well, using Sleep in a UI thread is a nuisance to the user because it blocks interaction. In this case, being a background service (no UI), it's not a bad way to go. Unless you can get rid of the necessity of periodically polling for changes in you database, you must release the processor. You seem to use a busywait loop which utilizes the CPU to its capacity doing nothing at all (most of the time). Alternatively you could try some sort of timer. Cheers
I don't like my signature at all
-
Hello, this is Vikash Gohil. I have Created a Windows Service which does the following when service is started : Connects SQL Server 2000 Database. Starts a Threads. Thread executes a while loop In the while loop, Current system time is taken using : format(now,"HH:mm") A Query is made to a Table in Database to look for records as below. "Select From Where ='" & format(now,"HH:mm") & "'" if any record exists, then a particular procedure gets executed. The above code continues to run in a while loop 24/7 The problem I am facing is, the service takes aroung 98% CPU Usage. The Database Table contains only 20 to 25 records. I want to know why this thing is happening. Is it bcoz of the while loop. If yes, then what to use instead of while loop? I Can't change the Service functionality as it is a client requirement. Please can anyone guide me in this matter. Any help would be greatly appreciated. Thanks in Advance, awaiting a reply soon.
I Don't think you have any while loop problem. The problem could be that connecting with SQL Server 2000 Database. At the initial state it takes (connecting with SQL Server 2000) long time bcoz sql server is just starting. Try finding to sql server running state and then connect with that. Sleep is not an good idea.