系统进程无故停止 Windows Server 2016
System Process Stops for no reason 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:Files 和工作目录工作正常
Task.Run(() => startTraining());
此方法用于异步调用此任务是否会在一段时间后关闭?
它会在 20 分钟后停止吗?如果是,请检查您的 ISS 池优势设置并使空闲时间等待 0 或更长的时间
您将需要同时读取标准输出和错误。 Likekey 正在发生的事情是标准错误的缓冲区变满并且程序冻结直到它从中读取某些东西(什么都不会)。
您需要在另一个线程上读取您的标准错误,或者您需要使用 event based version of reading standard error 来读取您的错误并将您的代码写入其中的文件。请参阅该 link 的备注部分,了解有关您需要执行哪些操作才能使其正常工作的具体说明。
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:Files 和工作目录工作正常
Task.Run(() => startTraining());
此方法用于异步调用此任务是否会在一段时间后关闭?
它会在 20 分钟后停止吗?如果是,请检查您的 ISS 池优势设置并使空闲时间等待 0 或更长的时间
您将需要同时读取标准输出和错误。 Likekey 正在发生的事情是标准错误的缓冲区变满并且程序冻结直到它从中读取某些东西(什么都不会)。
您需要在另一个线程上读取您的标准错误,或者您需要使用 event based version of reading standard error 来读取您的错误并将您的代码写入其中的文件。请参阅该 link 的备注部分,了解有关您需要执行哪些操作才能使其正常工作的具体说明。