![](/img/trans.png)
[英]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.