[英]Windows Server 2016 C# Launched process window is blank and unusable
[英]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:文件和工作目录工作正常
Task.Run(() => startTraining());
此方法用于临时调用此任务,是在一段时间后关闭此任务吗?
它会在20分钟后停止吗? 如果是,请检查您的ISS池优势设置,并使空闲时间等待0或更长时间
您将需要同时读取标准输出和错误。 可能发生的情况是标准错误的缓冲区已满,并且程序冻结,直到从中读取某些内容为止(什么也不会)。
您需要在另一个线程上读取标准错误 ,或者需要使用读取标准错误的基于事件的版本来读取您的错误,然后将代码写入其中的文件。 请参阅该链接的“备注”部分,以获取有关使其生效所需的具体说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.