Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hi,

I have developed a windows service application, by using multi threading, multiprocessing too.

Problem 1 : After service starts it takes the server into 100% CPU usage.

Problem 2 : If suppose i stop the service also, the process not stop, i can able to monitor that through task manager and resource monitor too..

Problem 3 : If no communication takes place with the service means also, the thread waiting in "Analysis Wait Chain"

Sample coding:
C#
while ((true && serverEndData != "END"))
{
    try
    {
      requestCount = requestCount + 1;
      NetworkStream networkStream = clientSocket.GetStream();
      networkStream.Read(bytesFrom, 0, 1);

      ss = Encoding.ASCII.GetString(bytesFrom, 0, bytesFrom.Length);
      fullRecDate = fullRecDate + ss.Trim();

      getData[rec] = bytesFrom[0];
      rec = rec + 1;

       if (rec == 48)
       {
          rec = 0;
          fullRecDate = DecryptTheData(getData);
          Log(fullRecDate, "Main", "Smart");
          string Str_DEcryptedData = string.Empty;
      
          for (int lo = 0; lo < 48; lo++)
          {
            byte a3 = getData[lo];
            int ij = Convert.ToInt32(a3);
            Str_DEcryptedData = Str_DEcryptedData + " " + DecimalToBase(ij);
          }

          string smartid = fullRecDate.Substring(0, 3);
          string hhid = fullRecDate.Substring(0, 3);
          string sbsend = fullRecDate.Substring(0, 4);

          if (smartid == "NSP")
          {
            try
            {
              string smartid = fullRecDate.Substring(5, 5);
              Log(fullRecDate, "NSP", smartid);
              string SPinfo2 = "";
              SPinfo2 = "NSP" + DatePacket + SPinfo1;

              SPinfo2 = SPinfo2.PadRight(48, '0');
              Log(SPinfo2, "NSP ACK", Smartid);  // smartid);

              byte[] encryptNsbData = EncryptTheDate(SBinfo2);
              networkStream.Write(encryptNsbData, 0, encryptNsbData.Length);
            }
            catch (Exception err)
            {
              Log(err.Message, "New SP", "SP");
            }
          }
          else
          {
            serverEndData = "END";
            networkStream.Flush();
            networkStream.Close(100);
            Log("END", "Server Closed Connection ", "");
          }
        }
      }
      catch (Exception ex)
      {
        Log(ex.Message, "ReadDataFromDevice[Main Communication]", "Smart");
        break;
      }
    }
  }
  catch
  {
    WriteLog("Application", "Smart", "Main Communication ", "Error");
  }
}


I have verified that while communication starts one thread is opening after the communication ends, it was closing properly, i have monitored by using system log file as well as service log file too. While continuous monitor of 5 hours, not extra threads running and not makes CPU into high usage, while not monitoring for 8 -10 hours gap, while we checking there will be unclosed threads will be maintain in the "Analysis wait Chain".
Posted
Updated 28-Jun-11 4:00am
v5
Comments
Kim Togo 27-Jun-11 3:44am    
Can you describe what is going on when the service starts and perhaps post some code?
Member 7744835 27-Jun-11 4:24am    
While starting the service, it showing normal usage only, while communication starts using telnet it starts to peak of CPU Usage. after we exit the telnet, the service usage is not getting down. it maintaining same level until the server becomes to restart
Kim Togo 27-Jun-11 4:37am    
Then the problem is the connection and communication method/class that handles the telnet session. High CPU usage can be a while or for loop that never ends.
Ankur\m/ 27-Jun-11 7:56am    
[moved from direct comment to the question]
OP wrote:
Yes, i have checked the service, all the loop has been ended properly, the main problem is that, while i install the service in my individual system and work means it was taking upto 12-15% of CPU Usage. but if i install it in server it was taking peak of CPU usage.
Member 7744835 28-Jun-11 8:04am    
Yes, i have checked the service, all the loop has been ended properly, the main problem is that, while i install the service in my individual system and work means it was taking upto 12-15% of CPU Usage. but if i install it in server it was taking peak of CPU usage.

Did there will be any possibility to not end any thread?

Actually while monitoring in system log and service log, there is no exception, but communication working properly for some hours, after that one thread makes it pending. but while monitoring in service log, i can able to find out that communication properly ended. what may be the reason?

1 solution

I am not so much sure.. but can you use timer or something instead of this while loop
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900