我创建了一个Windows服务,但它占用了99%或cpu的使用量,是否需要在c#代码中调用一些需要完成的工作?
就像是存在某种“空闲”状态,所以服务仍在监听端口但是没有吃掉如此多的CPU使用量?
修改
if (store_number.ToString().Length >= 3)
{
store_no_Folder = store_number.ToString();
try
{
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder))
{
logInfor.log("Creating: " + AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder);
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder);
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder + archived);
try
{
addst.aaddStation(ecm_path + "\\stations.xml", store_no_Folder, ecm_path);
}
catch (Exception ss)
{
logInfor.log(ss.ToString());
}
}
}
catch (Exception ds)
{
logInfor.log(ds.ToString());
}
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder + archived))
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder + archived);
}
try
{
doc.Save(AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder + archived + "\\" + dateTime + "_SO.xml");
doc.Save(AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder + "\\" + dateTime + "SO.xml");
logInfor.log("Saving SO.xml to designated folder: " + stored_xmls);
string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder);
for (int v = 0; v < files.Count(); v++)
{
/*fileN = files[v];*/
while (files[v].Contains("\\"))
{
files[v] = (files[v].Substring(files[v].IndexOf('\\') + 1));
}
logInfor.log("Moving :" + AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder + "\\" + files[v] + " ---> " + ecm_path + "\\Polling\\001" + store_no_Folder + "A\\OUT\\" + files[v]);
addst.moveFiles(files[v], AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder, ecm_path + "\\Polling\\001" + store_no_Folder + "A\\OUT\\");
}
DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + locations + store_no_Folder);
FileInfo[] Ffiles = di.GetFiles("*.xml")
.Where(p => p.Extension == ".xml").ToArray();
foreach (FileInfo file in Ffiles)
try
{
logInfor.log("Deleting: " + file.ToString());
file.Attributes = FileAttributes.Normal;
File.Delete(file.FullName);
}
catch { }
response = "Success";
}
catch (Exception arg)
{
response = "Failed";
logInfor.log(arg.ToString());
}
logInfor.log("MESSAGE TO CLIENT CALLING METHOD: " + response);
return response;
}
else
{
logInfor.log("Store number is out of range");
response = "Store numbers have exceeded 999";
return response;
}
}
catch (Exception err)
{
response = "Failed";
logInfor.log(err.ToString());
logInfor.log("MESSAGE TO CLIENT CALLING METHOD: " + response);
return response;
我添加了代码。
答案 0 :(得分:0)
我发现问题出现在代码的不同部分这是可以的,但它基本上是一个例外我没有抓住修复它现在它工作正常。
感谢大家的帮助