예제 #1
0
 private static bool RunProcess(ProcessProxy process)
 {
     try
     {
         if (!process.Start()) // process reuse is not expected
         {
             Debug.WriteLine($"Cannot start {process.StartInfo.FileName}");
             return(false);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         return(false);
     }
     return(true);
 }
예제 #2
0
        public static ProcessProxy CreateSdbProcess(bool createNoWindow = true, bool isEnableRasingEvent = false)
        {
            string sdbPath = SdbFilePath;

            if (string.IsNullOrEmpty(sdbPath) || File.Exists(sdbPath) == false)
            {
                return(null);
            }

            var sdbProcess = new ProcessProxy();

            sdbProcess.StartInfo.UseShellExecute        = false;
            sdbProcess.StartInfo.FileName               = sdbPath;
            sdbProcess.StartInfo.Verb                   = "runas";
            sdbProcess.StartInfo.RedirectStandardInput  = true;
            sdbProcess.StartInfo.RedirectStandardOutput = true;
            sdbProcess.StartInfo.RedirectStandardError  = true;
            sdbProcess.StartInfo.CreateNoWindow         = createNoWindow;
            sdbProcess.EnableRaisingEvents              = isEnableRasingEvent;

            return(sdbProcess);
        }
예제 #3
0
 public SdbCommand(ProcessProxy parent)
 {
     Parent  = parent;
     Command = string.Empty;
 }
예제 #4
0
        public static SdbRunResult RunSdbCommand(SDBDeviceInfo device, string command,
                                                 OutputDataProcessor outputDataProcessor, out int exitCode, TimeSpan?timeout = null)
        {
            Debug.Assert((timeout == null) || (timeout >= TimeSpan.Zero));

            exitCode = -1;
            TimeSpan effectiveTimeout = timeout ?? TimeSpan.FromSeconds(30); // the default timeout is 30 seconds

            using (ProcessProxy process = CreateSdbProcess(true, false))
            {
                if (process == null)
                {
                    return(SdbRunResult.CreateProcessError);
                }
                process.StartInfo.Arguments = DeviceManager.AdjustSdbArgument(device, command);
                Debug.WriteLine("{0} RunSdbCommand command '{1}'", DateTime.Now, process.StartInfo.Arguments);
                if (outputDataProcessor != null)
                {
                    object eventsGuard    = new object();
                    var    gotOutputEvent = new ManualResetEvent(false);
                    try
                    {
                        bool stopped = false; // should be volatile actually but it's not allowed for locals
                        process.OutputDataReceived += (sender, args) =>
                        {
                            if (!stopped && (args.Data != null))
                            {
                                lock (eventsGuard)
                                {
                                    if (outputDataProcessor == null)
                                    {
                                        return;
                                    }
                                    if (outputDataProcessor(true, args.Data))
                                    {
                                        gotOutputEvent.Set();
                                        stopped = true;
                                    }
                                }
                            }
                        };
                        process.ErrorDataReceived += (sender, args) =>
                        {
                            if (!stopped && (args.Data != null))
                            {
                                lock (eventsGuard)
                                {
                                    if (outputDataProcessor == null)
                                    {
                                        return;
                                    }
                                    if (outputDataProcessor(false, args.Data))
                                    {
                                        gotOutputEvent.Set();
                                        stopped = true;
                                    }
                                }
                            }
                        };
                        if (!RunProcess(process))
                        {
                            return(SdbRunResult.RunProcessError);
                        }
                        process.BeginOutputReadLine();
                        process.BeginErrorReadLine();
                        Stopwatch watch = Stopwatch.StartNew();
                        do
                        {
                            try
                            {
                                if (process.WaitForExit(0))
                                {
                                    process.WaitForExit(); // wait until redirected stdin/stdout streams are processed
                                    exitCode = process.ExitCode;
                                    return(SdbRunResult.Success);
                                }
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                                return(SdbRunResult.OtherError);
                            }
                        }while (watch.Elapsed < effectiveTimeout);
                        if (!process.HasExited)
                        {
                            process.Kill();
                        }
                    }
                    finally
                    {
                        lock (eventsGuard)
                        {
                            outputDataProcessor = null;
                        }
                        gotOutputEvent.Dispose();
                    }
                }
                else // outputDataProcessor == null
                {
                    if (!RunProcess(process))
                    {
                        return(SdbRunResult.RunProcessError);
                    }
                    try
                    {
                        double timeoutMilliseconds = effectiveTimeout.TotalMilliseconds;
                        if (process.WaitForExit((timeoutMilliseconds <= int.MaxValue) ? (int)timeoutMilliseconds : int.MaxValue))
                        {
                            exitCode = process.ExitCode;
                            return(SdbRunResult.Success);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        return(SdbRunResult.OtherError);
                    }
                }
            }
            return(SdbRunResult.Timeout);
        }