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. C#
  4. Thread taking maximum CPU time-

Thread taking maximum CPU time-

Scheduled Pinned Locked Moved C#
questiondata-structureshelp
8 Posts 6 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.
  • I Offline
    I Offline
    IamHuM
    wrote on last edited by
    #1

    hi... In my application , i'm using a thread to check for Queue contents. If i have any data then i have to transmit/receive that data. But my application is taking maximum CPU time if i'm using infinite for loop in this thread & because of this my CPU is using maximum time for my application(around 95%). & if i'm not using this for loop i am not able to transmit/receive whatever data is there in queue. private void ComportThread() { for (; ; ) { //Check for queue contains, if data is there then trnasmit/receive } } What is the problem...? Thanking you, Vinay

    E A S K 4 Replies Last reply
    0
    • I IamHuM

      hi... In my application , i'm using a thread to check for Queue contents. If i have any data then i have to transmit/receive that data. But my application is taking maximum CPU time if i'm using infinite for loop in this thread & because of this my CPU is using maximum time for my application(around 95%). & if i'm not using this for loop i am not able to transmit/receive whatever data is there in queue. private void ComportThread() { for (; ; ) { //Check for queue contains, if data is there then trnasmit/receive } } What is the problem...? Thanking you, Vinay

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

      Within your for loop, place a Thread.Sleep :)

      B 1 Reply Last reply
      0
      • E ejuanpp

        Within your for loop, place a Thread.Sleep :)

        B Offline
        B Offline
        bahman2000b
        wrote on last edited by
        #3

        See in a method which is working with thread if we have a loop we must use some code to stop the loop. for example i wrote the following code which passes to ThreadStart: for(;;) { bytesRcvd = client.Receive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None); msg1 = Encoding.UTF8.GetString(rcvBuffer); msg3 = "\0"; msg1 = msg1.Trim(msg3.ToCharArray()); Array.Clear(rcvBuffer, 0, rcvBuffer.Length); } the thread is await until the data received if we wrote a code in loop that has no stopping, CPU will be busy by 100%. Bahman

        1 Reply Last reply
        0
        • I IamHuM

          hi... In my application , i'm using a thread to check for Queue contents. If i have any data then i have to transmit/receive that data. But my application is taking maximum CPU time if i'm using infinite for loop in this thread & because of this my CPU is using maximum time for my application(around 95%). & if i'm not using this for loop i am not able to transmit/receive whatever data is there in queue. private void ComportThread() { for (; ; ) { //Check for queue contains, if data is there then trnasmit/receive } } What is the problem...? Thanking you, Vinay

          A Offline
          A Offline
          Ami Bar
          wrote on last edited by
          #4

          You can use a synchronizer, like semaphore to avoid the CPU usage of the ComportThread(). The ComportThread will wait on a semaphore. The producer will signal the semaphore when an object is queued. This way the ComportThread will wake up only when it is needed. Ami

          1 Reply Last reply
          0
          • I IamHuM

            hi... In my application , i'm using a thread to check for Queue contents. If i have any data then i have to transmit/receive that data. But my application is taking maximum CPU time if i'm using infinite for loop in this thread & because of this my CPU is using maximum time for my application(around 95%). & if i'm not using this for loop i am not able to transmit/receive whatever data is there in queue. private void ComportThread() { for (; ; ) { //Check for queue contains, if data is there then trnasmit/receive } } What is the problem...? Thanking you, Vinay

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            How do you do the checking of the queue to find if data is available? I'd suggest that you use some synchoronization mechanism like Monitor[^]. This would result in the thread not getting scheduled until there is data available in the queue.

            Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

            1 Reply Last reply
            0
            • I IamHuM

              hi... In my application , i'm using a thread to check for Queue contents. If i have any data then i have to transmit/receive that data. But my application is taking maximum CPU time if i'm using infinite for loop in this thread & because of this my CPU is using maximum time for my application(around 95%). & if i'm not using this for loop i am not able to transmit/receive whatever data is there in queue. private void ComportThread() { for (; ; ) { //Check for queue contains, if data is there then trnasmit/receive } } What is the problem...? Thanking you, Vinay

              K Offline
              K Offline
              Karthik Kalyanasundaram
              wrote on last edited by
              #6

              Using a ManualResetEvent will be good for these kind of problems. in ur looop call WaitOne() on ManulResetEvent object before check or getting data from queue. And in the thread where you add the Data to queue you must Set the ManulResetEvent object. Queue processing thread... while(true) { newDataAdded.WaitOne(); //newDataAdded an instance of ManualResetEvent. //code to get data from queue. //code to process ur data. //now check the size of the queue. if (queue.Count == 0) { newDataAdded.Reset(); } } And in the code where you add Data items to queue write code to set the ManualResetEvent object like this, public void AddData() { //Add data to the queue. newDataAdded.Set(); //do ur rest of the processings here. }

              I 1 Reply Last reply
              0
              • K Karthik Kalyanasundaram

                Using a ManualResetEvent will be good for these kind of problems. in ur looop call WaitOne() on ManulResetEvent object before check or getting data from queue. And in the thread where you add the Data to queue you must Set the ManulResetEvent object. Queue processing thread... while(true) { newDataAdded.WaitOne(); //newDataAdded an instance of ManualResetEvent. //code to get data from queue. //code to process ur data. //now check the size of the queue. if (queue.Count == 0) { newDataAdded.Reset(); } } And in the code where you add Data items to queue write code to set the ManualResetEvent object like this, public void AddData() { //Add data to the queue. newDataAdded.Set(); //do ur rest of the processings here. }

                I Offline
                I Offline
                IamHuM
                wrote on last edited by
                #7

                Hi... Here is complete code what i am doing... private void ComportThread() { for (; ; ) { if (MsgQueue.Count > 0) { string firstItem = (string)MsgQueue.Peek(); comport.Write(firstItem); MsgQueue.Dequeue(); } } comport is object of serialport class & MsgQueue is object of Queue... can u tell me now what is the probelm exactly... Regards, Vinay

                K 1 Reply Last reply
                0
                • I IamHuM

                  Hi... Here is complete code what i am doing... private void ComportThread() { for (; ; ) { if (MsgQueue.Count > 0) { string firstItem = (string)MsgQueue.Peek(); comport.Write(firstItem); MsgQueue.Dequeue(); } } comport is object of serialport class & MsgQueue is object of Queue... can u tell me now what is the probelm exactly... Regards, Vinay

                  K Offline
                  K Offline
                  Karthik Kalyanasundaram
                  wrote on last edited by
                  #8

                  The problem with ur Thread is repeatedly polling for Data in the queue. We can reduce the CPU utilization as far as possible by making ur thread work only when there is something in the queue and the way to do is explained in my previous post. USE A ManualResetEvent there in ur loop.

                  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