系统进程无故停止Windows Server 2016

时间:2018-11-23 11:59:59

标签: c# process asp.net-core-2.0 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());

此方法用于非常规地调用此任务,是否在一段时间后关闭了此任务?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您将需要同时从标准输出和错误中读取。可能发生的情况是标准错误的缓冲区已满,并且程序冻结,直到从中读取某些内容为止(什么也没有)。

您需要在另一个线程上读取标准错误,或者需要使用event based version of reading standard error来读取错误,然后将代码写入其中的文件。请参阅该链接的“备注”部分,以获取有关使其生效所需的具体说明。