首页 > 解决方案 > 系统进程无故停止 Windows Server 2016

问题描述

public void startTraining(bool initial)
    {
        int maxBatches = 100;
        int increment = 100;

        string ioFile = "";
        string ioFilePath = "C:\\pathOfCfg";

        while (maxBatches <= 5000)
        {
            if (maxBatches == increment)
            {

                string serverCmd = "/c HeavyProcessString;
                using (StreamWriter cmdFile = new StreamWriter(Path.Combine(rootPath + "\\" + project.ID + "\\File\\", "cmdCommands_" + maxBatches + ".txt")))
                {
                    cmdFile.WriteLine(serverCmd);
                }
                Process p = new Process();
                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.FileName = "C:\\Windows\\system32\\cmd.exe";
                p.StartInfo.Arguments = serverCmd;
                p.wait
                p.Start();

                try
                {
                    string op = p.StandardOutput.ReadToEnd();
                    using (StreamWriter outputFile = new StreamWriter(Path.Combine(rootPath + "\\" + project.ID + "\\Train", "output_" + maxBatches + ".txt")))
                    {
                        outputFile.WriteLine(op);
                    }
                }
                catch (Exception ex)
                {
                    string op = ex.ToString();
                    using (StreamWriter outputFile = new StreamWriter(Path.Combine(rootPath + "\\" + project.ID + "\\Train", "output_catch" + maxBatches + ".txt")))
                    {
                        outputFile.WriteLine(op);
                    }
                }

                try
                {
                    string ep = p.StandardError.ReadToEnd();
                    using (StreamWriter errorFile = new StreamWriter(Path.Combine(rootPath + "\\" + project.ID + "\\Train", "error_" + maxBatches + ".txt")))
                    {
                        errorFile.WriteLine(ep);
                    }
                }
                catch (Exception ex)
                {
                    string ep = ex.ToString();
                    using (StreamWriter errorFile = new StreamWriter(Path.Combine(rootPath + "\\" + project.ID + "\\Train", "error_catch" + maxBatches + ".txt")))
                    {
                        errorFile.WriteLine(ep);
                    }
                }

                ioFile =  maxBatches + "_.io";
                ioFile Path= rootPath + "\\" + project.ID + "\\File\\" + ioFile ;
                initial = false;
                p.Close();

            }
            else
            {
                string serverCmd = "/c HeavyProcessString;
                using (StreamWriter cmdFile = new StreamWriter(Path.Combine(rootPath + "\\" + project.ID + "\\Train", "cmdCommands_" + maxBatches + ".txt")))
                {
                    cmdFile.WriteLine(serverCmd);
                }

                Process p = new Process();
                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;

                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.FileName = "C:\\Windows\\system32\\cmd.exe";
                p.StartInfo.Arguments = serverCmd;
                p.Start();

                try
                {
                    string op = p.StandardOutput.ReadToEnd();
                    using (StreamWriter outputFile = new StreamWriter(Path.Combine(rootPath + "\\" + project.ID + "\\Train", "output_" + maxBatches + ".txt")))
                    {
                        outputFile.WriteLine(op);
                    }
                }
                catch (Exception ex)
                {
                    string op = ex.ToString();
                    using (StreamWriter outputFile = new StreamWriter(Path.Combine(rootPath + "\\" + project.ID + "\\Train", "output_catch" + maxBatches + ".txt")))
                    {
                        outputFile.WriteLine(op);
                    }
                }

                try
                {
                    string ep = p.StandardError.ReadToEnd();
                    using (StreamWriter errorFile = new StreamWriter(Path.Combine(rootPath + "\\" + project.ID + "\\Train", "error_" + maxBatches + ".txt")))
                    {
                        errorFile.WriteLine(ep);
                    }
                }
                catch (Exception ex)
                {
                    string ep = ex.ToString();
                    using (StreamWriter errorFile = new StreamWriter(Path.Combine(rootPath + "\\" + project.ID + "\\Train", "error_catch" + maxBatches + ".txt")))
                    {
                        errorFile.WriteLine(ep);
                    }
                }

                ioFile = maxBatches + "_.io";
                ioFilePath = rootPath + "\\" + project.ID + "\\File\\" + ioFile;
                p.Close();
            }
            maxBatches += increment;
        }
    }

我有一个类似在服务器上工作的函数,并且迭代地从进程中获取输出文件,然后在 5 次工作之后它停止提供输出。它什么也不写。我想如果进程停止或是否存在超时机制,或者进程是否因为内存不足而无法运行(但是当我从 cmd 运行它时,它在第 6 次迭代中运行良好)你对此有什么建议或智慧吗? ps:文件和工作目录工作正常

        Task.Run(() => startTraining());

此方法用于异步调用此任务是否会在一段时间后关闭?

标签: c#processasp.net-core-2.0windows-server-2016

解决方案


它会在 20 分钟内停止吗?如果是,请检查您的 ISS 池优势设置并使空闲时间等待 0 或更长的时间


推荐阅读