Windows Service应用程序-进程 [英] Windows Service Application - Process
问题描述
我也通过使用多线程,多处理开发了Windows服务应用程序.
问题1:服务启动后,它将服务器的CPU使用率提高到100%.
问题2:如果假设我也停止了该服务,但过程没有停止,我也可以通过任务管理器和资源监视器进行监视..
问题3:如果也未与服务工具进行通信,则在分析等待链"中等待的线程
样本代码:
同时((( true && serverEndData!= " ))
{
尝试
{
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 ;
如果(记录== 48 )
{
rec = 0 ;
fullRecDate = DecryptTheData(getData);
Log(fullRecDate," ," 智能");
字符串 Str_DEcryptedData = 字符串 .Empty;
for ( int lo = 0 ; lo < 48 ; lo ++)
{
字节 a3 = getData [lo];
int ij = Convert.ToInt32(a3);
Str_DEcryptedData = Str_DEcryptedData + " + DecimalToBase(ij);
}
字符串 smartid = fullRecDate.Substring( 0 , 3 );
字符串 hhid = fullRecDate.Substring( 0 , 3 );
字符串 sbsend = fullRecDate.Substring( 0 , 4 );
如果(聪明== " )
{
尝试
{
字符串 smartid = fullRecDate.Substring( 5 , 5 );
日志(fullRecDate," ,smartid);
字符串 SPinfo2 = " ;
SPinfo2 = " + DatePacket + SPinfo1;
SPinfo2 = SPinfo2.PadRight( 48 ,' 0');
日志(SPinfo2," ,Smartid); // smartid);
字节 [] encryptionNsbData = EncryptTheDate(SBinfo2);
networkStream.Write(encryptNsbData, 0 ,cryptoNsbData.Length);
}
捕获(异常err)
{
Log(err.Message," ," SP");
}
}
其他
{
serverEndData = " ;
networkStream.Flush();
networkStream.Close( 100 );
Log(" ," 服务器关闭连接", ");
}
}
}
捕获(例外)
{
Log(ex.Message," , 智能");
break ;
}
}
}
捕获
{
WriteLog(" ," 聪明", 主要沟通"," );
}
}
我已验证,虽然通信开始后通信结束后一个线程正在打开,但它正常关闭,所以我也使用系统日志文件和服务日志文件进行了监视.在连续监视5个小时的同时,不会运行额外的线程并且不会使CPU处于高利用率,而在不监视8 -10个小时的间隔的同时,我们将检查分析等待链"中是否存在未封闭的线程. class ="h2_lin">解决方案
我不太确定..但是在循环时可以使用计时器或其他方法代替此
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:
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".
I am not so much sure.. but can you use timer or something instead of this while loop
这篇关于Windows Service应用程序-进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!