Beispiel #1
0
        private string fLauch()
        {
            string _sResult = "";
            string _sError  = "";

            /*
             * if(!File.Exists(sExePath)){
             * Output.TraceError("Unable to lauch: " + sExePath);
             * }*/

            processStartInfo = new ProcessStartInfo(sExePath, sArg);
            processStartInfo.UseShellExecute = UseShellExecute;


            ExeProcess = new Process();



            if (bStartMinimised)
            {
                processStartInfo.WindowStyle = ProcessWindowStyle.Minimized;
            }
            if (bStartHidden)
            {
                processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            }

            if (bOutput && !bExterneLauch)
            {
                //processStartInfo.UseShellExecute = false;
                processStartInfo.UseShellExecute = false;
                processStartInfo.CreateNoWindow  = bHidden;



                //		processStartInfo.CreateNoWindow = true;
                processStartInfo.RedirectStandardOutput = bRedirectOutput;
                processStartInfo.RedirectStandardError  = true;
                processStartInfo.RedirectStandardInput  = true;
            }
            else
            {
                //   processStartInfo.CreateNoWindow = true;
                //    processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //    ExeProcess.StartInfo.CreateNoWindow = true;
                processStartInfo.UseShellExecute        = true;
                processStartInfo.RedirectStandardOutput = false;
                processStartInfo.RedirectStandardError  = false;
                processStartInfo.RedirectStandardInput  = false;
            }


            if (bRunAsAdmin)
            {
                //bRedirectOutput = false;
                //processStartInfo.UseShellExecute = true;
                processStartInfo.Verb = "runas"; //Run as admin
            }



            ExeProcess.StartInfo = processStartInfo;
            processStartInfo.WorkingDirectory = Path.GetDirectoryName(sWorkPath);

            bool processStarted = false;

            if (bStopAll)
            {
                bExeLauched = true;
                bExeLauch   = false;
                return("");
            }

            try
            {
                if (bHasError)
                {
                    return("");
                }

                Debug.fTrace("--------Lauch: " + sExePath + "  " + processStartInfo.Arguments);

                if (!File.Exists(sExePath))
                {
                    // Output.TraceError("No executable file found to lauch: \"" +  sExePath + "\" " + processStartInfo.Arguments);
                    Debug.TraceError("No executable file found to lauch: \"" + sExePath + "\" " + processStartInfo.Arguments);
                    return("");
                }

                //ExeProcess.StartInfo.CreateNoWindow = true;


                try
                {
                    processStarted = ExeProcess.Start();
                }
                catch (Exception e)
                {
                    Debug.TraceError("Unable to lauch: " + sExePath + " : " + e.Message);
                }



                ////////////////// OUTPUT /////////////////////////////

                if (bOutput && !bExterneLauch && !bWaitEndForOutput)
                {
                    ProcessOutputHandler outputHandler = new ProcessOutputHandler(this);
                    if (bRedirectOutput)
                    {
                        Thread stdOutReader = new Thread(new ThreadStart(outputHandler.ReadStdOut));
                        stdOutReader.Start();
                    }

                    Thread stdErrReader = new Thread(new ThreadStart(outputHandler.ReadStdErr));
                    stdErrReader.Start();
                    //stdOutReader.Priority = ThreadPriority.AboveNormal;
                    ExeProcess.WaitForExit(); //Stop here
                }
                if (bOutput && bRedirectOutput)
                {
                    _sResult += ExeProcess.StandardOutput.ReadToEnd();
                }
                if (bOutput)
                {
                    _sError += ExeProcess.StandardError.ReadToEnd();
                }
                if (bWaitEndForOutput)
                {
                    ExeProcess.WaitForExit();
                }

                if (bRedirectOutput && dOut != null && _sResult != "")
                {
                    dOut(this, _sResult);
                }
                if (dError != null && _sError != "")
                {
                    dError(this, _sError);
                }
                ///////////////////////////////////////////////////////////



                if (bDontKill)
                {
                    //     Data.nDontKillId = ExeProcess.Id;
                }



                bExeLauched = true;
                if (oFromWindow != null)
                {
                    Console.Write("\n ExeProcess.MainWindowHandle: " + ExeProcess.MainWindowHandle);

                    while (ExeProcess.MainWindowHandle == IntPtr.Zero)
                    {
                        Thread.Sleep(1);
                        ExeProcess.Refresh();
                    }

                    // ShowWindow(ExeProcess.MainWindowHandle, SW_HIDE);

                    //   ShowWindow(ExeProcess.MainWindowHandle, SW_RESTORE);
                    //    ShowWindow(ExeProcess.MainWindowHandle, SW_HIDE);

                    if (sArg != "")
                    {
                        string _sTitle = ExeProcess.MainWindowTitle;
                        while (bExeLauched && !ExeProcess.HasExited)
                        {
                            if (oFromWindow != null)
                            {
                                // WinApi.GetWindowInfo
                                //  Console.WriteLine("Title: " + ExeProcess.MainWindowTitle);
                                ExeProcess.Refresh();
                                //  break;
                                if (_sTitle != ExeProcess.MainWindowTitle)
                                {
                                    break;
                                }

                                /*
                                 * Rect r = new Rect();
                                 * GetWindowRect(ExeProcess.MainWindowHandle, ref r);
                                 * if ((r.Left != t.Left || r.Top != t.Top || r.Right != t.Right || r.Bottom != t.Bottom))
                                 * {
                                 *  t = r;
                                 *  _nCountRes++;
                                 *  Console.WriteLine(r.Left + ":" + r.Right + ":" + r.Top + ":" + r.Bottom);
                                 *  if (_nCountRes == 2) {
                                 * //        break;
                                 *  }
                                 *
                                 * }*/
                            }
                            Thread.Sleep(10);//4 frames
                        }


                        Thread.Sleep(1);
                        Thread.Sleep(1000); //Remove Popup
                    }
                    oFromWindow.fSetParent(ExeProcess.MainWindowHandle);

                    ShowWindow(ExeProcess.MainWindowHandle, SW_SHOW);
                }


                sResult = _sResult;
                sError  = _sError;



                while (bExeLauched && !ExeProcess.HasExited)
                {
                    Thread.Sleep(1);
                }
                if (dExit != null)
                {
                    dExit(this);
                }
            }
            catch (Exception ex)
            {
                Debug.fTrace(ex.Message);
            }

            bExeLauch = false;


            //           Debug.fTrace("--------------------------- 7z finish -------------------------");

            if (bReturnBoth)
            {
                return(_sResult + "\n" + _sError);
            }
            else
            {
                if (!bReturnError)
                {
                    return(_sResult);
                }
                else
                {
                    return(_sError);
                }
            }
        }
Beispiel #2
0
        private string fLaunch()
        {
            string _sResult = "";
            string _sError  = "";

            /*
             *                  if(!File.Exists(sExePath)){
             *                          Output.TraceError("Unable to Launch: " + sExePath);
             *                  }*/

            processStartInfo = new ProcessStartInfo(Path.GetFullPath(sExePath), sArg);
            processStartInfo.UseShellExecute = UseShellExecute;

//bWaitEndForOutput = true;


//Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;

            ExeProcess = new Process();
            //    ExeProcess.EnableRaisingEvents = true;
            //    ExeProcess.Exited += new EventHandler(fExited);


            if (bOutput)
            {
                if (!bExterneLaunch)
                {
                    //processStartInfo.UseShellExecute = false;
                    processStartInfo.UseShellExecute = false;
                    processStartInfo.CreateNoWindow  = bHidden;
                    // processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    //		processStartInfo.CreateNoWindow = true;
                    processStartInfo.RedirectStandardOutput = bRedirectOutput;
                    processStartInfo.RedirectStandardError  = true;
                    processStartInfo.RedirectStandardInput  = true;
                }

/*
 *                                              if(!bWaitEndForOutput) {
 *                                                      ExeProcess.OutputDataReceived += (sender, e) => {
 *                                                                      if (e.Data != null)  {
 *                                                                              fAppOutput(this, e.Data);
 *                                                                      }
 *                                                              };
 *                                                      ExeProcess.ErrorDataReceived += (sender, e) => {
 *                                                              if (e.Data != null)  {
 *                                                                      fAppError(this, e.Data);
 *                                                              }
 *                                                      };
 *                                              }*/
            }

            ExeProcess.StartInfo = processStartInfo;
            processStartInfo.WorkingDirectory = Path.GetDirectoryName(sWorkPath);

            bool processStarted = false;

            if (bStopAll)
            {
                bExeLaunched = true;
                bExeLaunch   = false;
                return("");
            }

            try {
                if (bHasError)
                {
                    return("");
                }

                Debug.fTrace("--------Launch: " + sExePath + "  " + processStartInfo.Arguments);

                if (!File.Exists(sExePath))
                {
                    Output.TraceError("No executable file found to Launch: \"" + sExePath + "\" " + processStartInfo.Arguments);
                    return("");
                }


                //Debug.fTrace("Arguments: " +   processStartInfo.Arguments );
                //	Debug.fTrace("WorkingDirectory***: " + processStartInfo.WorkingDirectory );


                try{
                    fAddThisToList();
                    processStarted = ExeProcess.Start();
                } catch (Exception e) {
                    Output.TraceError("Unable to Launch: " + sExePath + " [" + sWorkPath + "] : " + e.Message);
                }


                if (bDontKill)
                {
                    Data.nDontKillId = ExeProcess.Id;
                }
                bExeLaunched = true;


                if (!bExterneLaunch && !bWaitEndForOutput)
                {
                    ProcessOutputHandler outputHandler = new ProcessOutputHandler(this);

                    if (bRedirectOutput)
                    {
                        Thread stdOutReader = new Thread(new ThreadStart(outputHandler.ReadStdOut));
                        stdOutReader.Start();
                    }

                    Thread stdErrReader = new Thread(new ThreadStart(outputHandler.ReadStdErr));
                    stdErrReader.Start();
//stdOutReader.Priority = ThreadPriority.AboveNormal;
                    ExeProcess.WaitForExit();             //Stop here
                }


                //	if(bWaitEndForOutput) {
                if (bRedirectOutput)
                {
                    _sResult += ExeProcess.StandardOutput.ReadToEnd();
                }
                _sError += ExeProcess.StandardError.ReadToEnd();

                if (bWaitEndForOutput)
                {
                    ExeProcess.WaitForExit();
                }

                if (bRedirectOutput && dOut != null && _sResult != "")
                {
                    dOut(this, _sResult);
                }
                if (dError != null && _sError != "")
                {
                    dError(this, _sError);
                }

/*
 *                                              }else {
 *                                                       ExeProcess.WaitForExit();
 *                                                      _sResult +=  ExeProcess.StandardOutput.ReadToEnd();
 *                                                      _sError +=  ExeProcess.StandardError.ReadToEnd();
 *                                                      if(dOut != null && _sResult != ""){
 *                                                              dOut(this, _sResult);
 *                                                      }
 *                                                      if(dError != null  && _sError != ""){
 *                                                              dError(this, _sError);
 *                                                      }*/



                /*
                 *      while(!ExeProcess.HasExited){
                 *              // if (!ExeProcess.StandardOutput.EndOfStream){
                 *                      string _sOut = ExeProcess.StandardOutput.ReadLine();
                 *                      if(dOut != null && _sOut != ""){
                 *                              dOut(this, _sOut);
                 *                      }
                 *              //}
                 *              //if (!ExeProcess.StandardOutput.EndOfStream){
                 *                      string _sErr = ExeProcess.StandardError.ReadLine();
                 *                      if(dError != null  && _sErr != ""){
                 *                              dError(this, _sErr);
                 *                      }
                 *              //}
                 *              Thread.Sleep(1);
                 *      }
                 *      _sResult +=  ExeProcess.StandardOutput.ReadToEnd();
                 *      _sError +=  ExeProcess.StandardError.ReadToEnd();
                 *
                 *       ExeProcess.WaitForExit();
                 *      if(dOut != null && _sResult != ""){
                 *              dOut(this, _sResult);
                 *      }
                 *      if(dError != null  && _sError != ""){
                 *              dError(this, _sError);
                 *      }*/

                /*
                 * if(bOutput) {
                 *       ExeProcess.BeginOutputReadLine();///SLOWWW
                 *       ExeProcess.BeginErrorReadLine();
                 * }*/

                //	}


                ExeProcess.WaitForExit();                              //important for geting last output!
                fRemoveThisFromList();

                sResult = _sResult;
                sError  = _sError;

                while (!ExeProcess.HasExited && Base.bAlive)
                {
                    Thread.CurrentThread.Join(1);
                }
                if (dExit != null)
                {
                    dExit(this);
                }
                if (bDontKill && ExeProcess.Id == Data.nDontKillId)
                {
                    Data.nDontKillId = 0;
                }



                /*
                 * if(oForm != null) {
                 *  oForm.fLaunchEnd();
                 * }*/
            }  catch (Exception ex) {
                Debug.fTrace(ex.Message);
            }

            bExeLaunch = false;


            //           Debug.fTrace("--------------------------- 7z finish -------------------------");

            if (bReturnBoth)
            {
                return(_sResult + "\n" + _sError);
            }
            else
            {
                if (!bReturnError)
                {
                    return(_sResult);
                }
                else
                {
                    return(_sError);
                }
            }
        }