बाहर निकलने के इंतजार के दौरान मेरी प्रक्रिया के लटकने का क्या कारण हो सकता है?
इस कोड को पॉवरशेल स्क्रिप्ट शुरू करनी होती है, जिसके अंदर कई कार्रवाई करता है। जैसे MSBuild के माध्यम से कोड को फिर से जमा करना शुरू करता है, लेकिन शायद समस्या यह है कि यह बहुत अधिक आउटपुट उत्पन्न करता है और पावर शेल स्क्रिप्ट को सही तरीके से निष्पादित करने के बाद भी बाहर निकलने के इंतजार में यह कोड अटक जाता है।
यह थोड़े "अजीब" है क्योंकि कभी-कभी यह कोड ठीक काम करता है और कभी-कभी यह बस अटक जाता है।
कोड हैंग हो जाता है:
process.WaitForExit (ProcessTimeOutMiliseconds);
Powershell script 1-2sec की तरह निष्पादित होती है, इस बीच का समय 19sec है।
public static (bool Success, string Logs) ExecuteScript(string path, int ProcessTimeOutMiliseconds, params string[] args)
{
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
using (var outputWaitHandle = new AutoResetEvent(false))
using (var errorWaitHandle = new AutoResetEvent(false))
{
try
{
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "powershell.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
Arguments = $"-ExecutionPolicy Bypass -File \"{path}\"",
WorkingDirectory = Path.GetDirectoryName(path)
};
if (args.Length > 0)
{
var arguments = string.Join(" ", args.Select(x => $"\"{x}\""));
process.StartInfo.Arguments += $" {arguments}";
}
output.AppendLine($"args:'{process.StartInfo.Arguments}'");
process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit(ProcessTimeOutMiliseconds);
var logs = output + Environment.NewLine + error;
return process.ExitCode == 0 ? (true, logs) : (false, logs);
}
}
finally
{
outputWaitHandle.WaitOne(ProcessTimeOutMiliseconds);
errorWaitHandle.WaitOne(ProcessTimeOutMiliseconds);
}
}
}
स्क्रिप्ट:
start-process $args[0] App.csproj -Wait -NoNewWindow
[string]$sourceDirectory = "\bin\Debug\*"
[int]$count = (dir $sourceDirectory | measure).Count;
If ($count -eq 0)
{
exit 1;
}
Else
{
exit 0;
}
कहाँ पे
$args[0] = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe"
संपादित करें
@ Ingen के समाधान के लिए मैंने छोटे रैपर जोड़े जो एम.एस.
public static void ExecuteScriptRx(string path, int processTimeOutMilliseconds, out string logs, out bool success, params string[] args)
{
var current = 0;
int attempts_count = 5;
bool _local_success = false;
string _local_logs = "";
while (attempts_count > 0 && _local_success == false)
{
Console.WriteLine($"Attempt: {++current}");
InternalExecuteScript(path, processTimeOutMilliseconds, out _local_logs, out _local_success, args);
attempts_count--;
}
success = _local_success;
logs = _local_logs;
}
जहाँ InternalExecuteScriptकोड कूट है
Rxकाम किया (जैसा कि इसमें कोई समय नहीं लगा था) यहां तक कि आवारा MSBuild प्रक्रिया के साथ अनिश्चित प्रतीक्षा के लिए अग्रणी है? यह जानने की रुचि है कि कैसे संभाला गया