Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Windows Service Comsuming 99% CPU

Windows Service Comsuming 99% CPU

Scheduled Pinned Locked Moved Visual Basic
databasehelpsql-serversysadmintutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VikashGohil
    wrote on last edited by
    #1

    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.

    E S 2 Replies Last reply
    0
    • V VikashGohil

      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.

      E Offline
      E Offline
      Estys
      wrote on last edited by
      #2

      Consider using System.Threading.Thread.Sleep( int milliseconds ). That should unburden the processor. Cheers

      I don't like my signature at all

      V 1 Reply Last reply
      0
      • E Estys

        Consider using System.Threading.Thread.Sleep( int milliseconds ). That should unburden the processor. Cheers

        I don't like my signature at all

        V Offline
        V Offline
        VikashGohil
        wrote on last edited by
        #3

        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.

        D E 2 Replies Last reply
        0
        • V VikashGohil

          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.

          D Offline
          D Offline
          Dalek Dave
          wrote on last edited by
          #4

          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

          V 1 Reply Last reply
          0
          • D 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

            V Offline
            V Offline
            VikashGohil
            wrote on last edited by
            #5

            Hello, Dalek. Thanks for Reply. I will surely try using Sleep. Thanks.

            1 Reply Last reply
            0
            • V VikashGohil

              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.

              E Offline
              E Offline
              Estys
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • V VikashGohil

                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.

                S Offline
                S Offline
                slam Iqbal
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups