c# - 系统进程无故停止 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());
此方法用于异步调用此任务是否会在一段时间后关闭?
解决方案
它会在 20 分钟内停止吗?如果是,请检查您的 ISS 池优势设置并使空闲时间等待 0 或更长的时间
推荐阅读
- powershell - 我正在尝试将“返回字符串问题中的第一个重复字符”从 python 转换为 powershell
- javascript - Display:none 在某些浏览器中不起作用
- python - Shapely 无法识别 geoJson 的几何类型
- python - 询问用户姓名和三个等级,然后计算平均值
- python - 如何使用 Selenium Python 无头运行 Microsoft Edge?
- python - 与重复的组合
- php - apache拒绝运行php
- python - 删除列表Python中的字符串字符
- c - My program doesn't output the first value I entered to search in my array. Why?
- swiftui - 当当前时间超过 SwiftUI 中的阈值时,如何更新视图?