Windows服务占用了99%的CPU使用率

本文关键字:CPU 使用率 服务 Windows | 更新日期: 2023-09-27 18:33:04

我创建了一个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;

我添加了代码。

Windows服务占用了99%的CPU使用率

我发现问题出在代码的不同部分,这没关系,但这基本上是一个例外,我没有抓住修复它,现在它工作正常。

感谢大家的帮助