快速版本:在我的项目中,我有一个命令行工具,可以捕获计算机上所有窗口的屏幕快照(用于测试产品的Bug收集工具)。这是命令行CollectSystemLogs.exe
的一部分,该命令行收集了很多东西,屏幕截图只是其中一项。
我有一个小的UI(CollectUSLogs.exe
),测试人员/用户可以选择要选择的项目。这只是一个UI前端,CollectSystemLogs.exe
命令行完成了所有实际工作(带有告诉它要收集什么的参数)。
我可以从命令行运行命令行CollectSystemLogs.exe
,所有屏幕截图都可以收集到。
但是,当我运行UI工具CollectUSLogs.exe并选择屏幕截图时,它只会抓住一些,然后似乎挂起了。它停止了,我不知道为什么。不幸的是,因为它是由UI启动的过程,所以我无法对其进行调试,并且如果我仅运行命令行,它就可以工作。
相关代码:
这是用于收集屏幕截图的代码(请忽略所有详细的日志记录.....进行老式的printf调试)。
/// <summary>Gets images of each window on the screen.</summary>
public static void CaptureWindows(string savePath)
{
LogManager.LogDebugMessage($"Starting CaptureWindows({savePath})");
AutomationElementCollection desktopChildren = AutomationElement.RootElement.FindAll(TreeScope.Children, Condition.TrueCondition);
int windowCount = 1;
LogManager.LogDebugMessage($"{desktopChildren.Count} desktopChildren (windows) found.");
foreach (AutomationElement window in desktopChildren)
{
LogManager.LogDebugMessageConsole($"Capturing window [{window.Current.Name}]");
Rect rect = window.Current.BoundingRectangle;
if (Double.IsInfinity(rect.Width) || Double.IsInfinity(rect.Height))
{
LogManager.LogErrorMessageConsole($"[{window.Current.Name}] has at leat one infinite dimension.");
LogManager.LogErrorMessageConsole($"w: {rect.Width}, h: {rect.Height}");
}
try
{
// TODO: Get rid of unneeded debug log prints
LogManager.LogDebugMessage("In try{}");
using (var bitmap = new Bitmap((int)rect.Width, (int)rect.Height))
{
LogManager.LogDebugMessage($"Bitmap Created {(int)rect.Width}x{(int)rect.Height}");
using (Graphics graphic = Graphics.FromImage(bitmap))
{
LogManager.LogDebugMessage($"Graphics created {graphic.ToString()}");
IntPtr handleDeviceContext = graphic.GetHdc();
var hwnd = (IntPtr)window.Current.NativeWindowHandle;
LogManager.LogDebugMessage($"hwnd created {hwnd.ToString()}");
if (hwnd == IntPtr.Zero) break;
NativeMethods.PrintWindow(hwnd, handleDeviceContext, 0);
LogManager.LogDebugMessage("PrintWindow() complete.");
graphic.ReleaseHdc(handleDeviceContext);
}
// Create File Name for image to be saved
string fileName = Path.Combine(savePath, windowCount++.ToString("Window00") + ".png");
LogManager.LogDebugMessage($"Saving {fileName}");
bitmap.Save(fileName, ImageFormat.Png);
LogManager.LogDebugMessage($"{fileName} saved");
}
LogManager.LogDebugMessage("End of try{}");
}
catch (Exception e)
{
LogManager.LogExceptionMessageConsole(e);
}
LogManager.LogDebugMessage("End of foreach");
}
LogManager.LogDebugMessage("Exiting CaptureWindows()");
}
我使用以下命令调用命令行:
if (!ProcessHelpers.RunCommandProcessCollectOutput(command, args, out output))
{
MessageBox.Show($"Error running command line tool to collect system logs. Please save {Path.Combine(LogManagerConstants.LogFilePath,LogManagerConstants.LogFileBasename)} for analysis.",
@"CollectSystemLogs.exe Execution Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
该代码在这里:
public static bool RunCommandProcessCollectOutput(string command, string args, out string output)
{
// cmd arg /c => run shell and then exit
// cmd arg /d => disables running of autorun commands from reg
// (may inject extra text into output that could affect parsing)
string localArgs = $"/d /c {command} {args}";
string localCommand = @"cmd";
if (command.StartsWith(@"\\"))
{
NetworkHelpers.CreateMapPath(Path.GetDirectoryName(command));
}
ProcessStartInfo procStartInfo = new ProcessStartInfo(localCommand, localArgs);
procStartInfo.UseShellExecute = false; // use shell (command window)
procStartInfo.CreateNoWindow = false; // Yes, create a window
procStartInfo.ErrorDialog = false; // Will not show error dialog if process can't start
procStartInfo.WindowStyle = ProcessWindowStyle.Normal; // Normal type window
procStartInfo.RedirectStandardOutput = true; // redirect stdout so we can capture
procStartInfo.RedirectStandardError = true; // redirect stderr so we can capture
return _RunProcessCollectOutput(procStartInfo, out output);
}
最后一块:
private static bool _RunProcessCollectOutput(ProcessStartInfo procStartInfo, out string output)
{
bool successful = true;
output = ""; // init before starting
LogManager.LogDebugMessage("_RunProcessCollectOutput");
try
{
// Create proc, assign ProcessStartInfo to the proc and start it
Process proc = new Process();
proc.StartInfo = procStartInfo;
// if collecting output, we must wait for process to end in order
// to collect output.
LogManager.LogDebugMessage($"Starting {procStartInfo.FileName} {procStartInfo.Arguments}");
LogManager.LogDebugMessage("[wait forever]");
successful = proc.Start();
string temp1 = proc.StandardOutput.ReadToEnd(); // return output if any
string temp2 = proc.StandardError.ReadToEnd(); // return error output if any
proc.WaitForExit(); // Wait forever (or until process ends)
if (temp1.Length > 0)
{
output += "[STDOUT]\n" + temp1 + "[/STDOUT]\n";
}
if (temp2.Length > 0)
{
successful = false;
output += "[STDERR]\n" + temp2 + "[/STDERR]\n";
}
}
catch (Exception e)
{
if (procStartInfo != null)
{
LogManager.LogErrorMessage($"Error starting the process {procStartInfo.FileName} {procStartInfo.Arguments}");
}
LogManager.LogExceptionMessage(e);
successful = false;
}
return successful;
}
所以,正如我说的那样,...从命令行运行时效果很好,但是当从UI内以这种方式调用该命令时,它似乎只会出现前几个窗口,然后挂起。
查看日志中的输出。它得到前几个(似乎是我拥有的三台显示器上的任务栏,然后挂起。它似乎停在第四台之后,即使它告诉我:
找到40个DesktopChildren(Windows)。
我猜第四个cmd窗口是运行该工具的窗口,但我不知道这应该有多重要。
[20190116164608|DBG|CollectUSLogs.exe]_RunProcessCollectOutput
[20190116164608|DBG|CollectUSLogs.exe]Starting cmd /d /c C:\XTT\UsbRoot\bin\CollectSystemLogs.exe -ss -dp D:\
[20190116164608|DBG|CollectUSLogs.exe][wait forever]
[20190116164608|DBG|CollectSystemLogs.exe]Argument: -ss
[20190116164608|DBG|CollectSystemLogs.exe]Argument: -dp
[20190116164608|DBG|CollectSystemLogs.exe]D:\
[20190116164608|ERR|CollectSystemLogs.exe]Could not find a part of the path 'e:\host\config\iu\systemoptions.xml'.
[20190116164608|ERR|CollectSystemLogs.exe] at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
[20190116164608|ERR|CollectSystemLogs.exe] at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRigh
[20190116164608|ERR|CollectSystemLogs.exe]ts, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Bo
[20190116164608|ERR|CollectSystemLogs.exe]olean bFromProxy, Boolean useLongPath, Boolean checkHost)
[20190116164608|ERR|CollectSystemLogs.exe] at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 buffe
[20190116164608|ERR|CollectSystemLogs.exe]rSize)
[20190116164608|ERR|CollectSystemLogs.exe] at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCac
[20190116164608|ERR|CollectSystemLogs.exe]hePolicy cachePolicy)
[20190116164608|ERR|CollectSystemLogs.exe] at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
[20190116164608|ERR|CollectSystemLogs.exe] at System.Xml.XmlTextReaderImpl.FinishInitUriString()
[20190116164608|ERR|CollectSystemLogs.exe] at System.Xml.XmlTextReaderImpl..ctor(String uriStr, XmlReaderSettings settings, XmlParserContext context
[20190116164608|ERR|CollectSystemLogs.exe], XmlResolver uriResolver)
[20190116164608|ERR|CollectSystemLogs.exe] at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext)
[20190116164608|ERR|CollectSystemLogs.exe] at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext
[20190116164608|ERR|CollectSystemLogs.exe])
[20190116164608|ERR|CollectSystemLogs.exe] at System.Xml.Linq.XDocument.Load(String uri, LoadOptions options)
[20190116164608|ERR|CollectSystemLogs.exe] at System.Xml.Linq.XDocument.Load(String uri)
[20190116164608|ERR|CollectSystemLogs.exe] at XTT.USCartHelpers.get_SerialNumber() in C:\XTT\XTT_Tools\XTT\Helpers\USCartHelpers.cs:line 64
[20190116164608|ERR|CollectSystemLogs.exe]Cannot find Registry32 HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Philips\PHC\Ultrasound
[20190116164608|WRN|CollectSystemLogs.exe]GetOptionalRegValue: Unable to find ProductModel.
[20190116164608|ERR|CollectSystemLogs.exe]Cannot find Registry32 HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Philips\PHC\Ultrasound
[20190116164608|WRN|CollectSystemLogs.exe]GetOptionalRegValue: Unable to find ProductProgram.
[20190116164608|DBG|CollectSystemLogs.exe]Zipfilename: D:\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND).zip
[20190116164608|DBG|CollectSystemLogs.exe]ZipFolder = C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND)
[20190116164608|WRN|CollectSystemLogs.exe]Can't empty C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND). It doesn't exist.
[20190116164608|INF|CollectSystemLogs.exe]C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND) created
[20190116164608|ERR|CollectSystemLogs.exe]Can't copy SystemOption*.xml in e:\host\config\iu. It doesn't exist.
[20190116164608|ERR|CollectSystemLogs.exe]ERROR collecting SystemOptions.
[20190116164608|ERR|CollectSystemLogs.exe]SystemOptions may not be included in zip file.
[20190116164609|DBG|CollectSystemLogs.exe]Starting CaptureWindows(C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND))
[20190116164624|DBG|CollectSystemLogs.exe]40 desktopChildren (windows) found.
[20190116164624|DBG|CollectSystemLogs.exe]Capturing window []
[20190116164624|DBG|CollectSystemLogs.exe]In try{}
[20190116164624|DBG|CollectSystemLogs.exe]Bitmap Created 1920x40
[20190116164624|DBG|CollectSystemLogs.exe]Graphics created System.Drawing.Graphics
[20190116164624|DBG|CollectSystemLogs.exe]hwnd created 2626768
[20190116164624|DBG|CollectSystemLogs.exe]PrintWindow() complete.
[20190116164624|DBG|CollectSystemLogs.exe]Saving C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND)\Window01.png
[20190116164624|DBG|CollectSystemLogs.exe]C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND)\Window01.png saved
[20190116164624|DBG|CollectSystemLogs.exe]End of try{}
[20190116164624|DBG|CollectSystemLogs.exe]End of foreach
[20190116164624|DBG|CollectSystemLogs.exe]Capturing window []
[20190116164624|DBG|CollectSystemLogs.exe]In try{}
[20190116164624|DBG|CollectSystemLogs.exe]Bitmap Created 1080x40
[20190116164624|DBG|CollectSystemLogs.exe]Graphics created System.Drawing.Graphics
[20190116164624|DBG|CollectSystemLogs.exe]hwnd created 66184
[20190116164624|DBG|CollectSystemLogs.exe]PrintWindow() complete.
[20190116164624|DBG|CollectSystemLogs.exe]Saving C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND)\Window02.png
[20190116164624|DBG|CollectSystemLogs.exe]C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND)\Window02.png saved
[20190116164624|DBG|CollectSystemLogs.exe]End of try{}
[20190116164624|DBG|CollectSystemLogs.exe]End of foreach
[20190116164624|DBG|CollectSystemLogs.exe]Capturing window []
[20190116164624|DBG|CollectSystemLogs.exe]In try{}
[20190116164624|DBG|CollectSystemLogs.exe]Bitmap Created 1920x40
[20190116164624|DBG|CollectSystemLogs.exe]Graphics created System.Drawing.Graphics
[20190116164624|DBG|CollectSystemLogs.exe]hwnd created 333194
[20190116164624|DBG|CollectSystemLogs.exe]PrintWindow() complete.
[20190116164624|DBG|CollectSystemLogs.exe]Saving C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND)\Window03.png
[20190116164624|DBG|CollectSystemLogs.exe]C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND)\Window03.png saved
[20190116164624|DBG|CollectSystemLogs.exe]End of try{}
[20190116164624|DBG|CollectSystemLogs.exe]End of foreach
[20190116164624|DBG|CollectSystemLogs.exe]Capturing window [C:\WINDOWS\SYSTEM32\cmd.exe]
[20190116164624|DBG|CollectSystemLogs.exe]In try{}
[20190116164624|DBG|CollectSystemLogs.exe]Bitmap Created 993x519
[20190116164624|DBG|CollectSystemLogs.exe]Graphics created System.Drawing.Graphics
[20190116164624|DBG|CollectSystemLogs.exe]hwnd created 269574
[20190116164624|DBG|CollectSystemLogs.exe]PrintWindow() complete.
[20190116164624|DBG|CollectSystemLogs.exe]Saving C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND)\Window04.png
[20190116164624|DBG|CollectSystemLogs.exe]C:\temp\20190116164608_TestCart00_(NOTFOUND)_(NOTFOUND)\Window04.png saved
[20190116164624|DBG|CollectSystemLogs.exe]End of try{}
[20190116164624|DBG|CollectSystemLogs.exe]End of foreach
任何想法或建议将不胜感激。
我创建了一个虚拟命令行,该虚拟命令行调用相同的命令行(CollectSystemLogs.exe),并且使用相同的方法调用RunCommandProcessCollectOutput()。
答案 0 :(得分:0)
好的...事实证明,@ Adam Plocher在第一次猜测中是正确的。这是UseShellExecute标志。
我正在使用包装器进行具有UseShellExecute = false的启动过程,因为这是重定向我想要的STDOUT和STDERR以便将它们放入我的日志所必需的。
我以为我以前曾经使用UseShellExecute = true尝试过,但是我认为当我尝试得到一个错误时,因为它仍在尝试重定向那些流,而且我必须在那儿停下来。
我使用了另一个具有UseShellExecute = true的包装器,但没有给我STDOUT和STDERR,它可以工作。我想我可以忍受,就像在我的代码中详细记录日志一样。
我仍然不知道为什么使用UseShellExecute = false运行进程时它会以这种方式运行;