Thread taking maximum CPU time-
-
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
-
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
-
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 -
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
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
-
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
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
-
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
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. }
-
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. }
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
-
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
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.