Ejemplo n.º 1
0
 public void Extract(string compresedFile, string uncompresedDir)
 {
     try
     {
         UnZipFromMTAThread(compresedFile, uncompresedDir);
     }
     catch (System.Exception e)
     {
         installationPackage.ErrorMessage = e.Message;
         installationPackage.OnInstallFailed(ErrorConsts.ERR_EXTRACT_GENERAL, installationPackage.ErrorMessage);
     }
     installationPackage.HandleExtractEnded();
 }
Ejemplo n.º 2
0
        internal void Run(string runName, string runParam = "")
        {
            _runnerThread = new Thread(() =>
            {
                lock (terminationLock)
                {
                    try
                    {
                        try
                        {
                            Process p = null;

#if DEBUG
                            Logger.GetLogger().Info($"Running {runName} {runParam}");
#endif
                            bool isRunnable = FileUtils.IsRunnableFile(runName);

                            if (_installationPackage.RunWithBits && !isRunnable)
                            {
#if DEBUG
                                Logger.GetLogger().Info($"{runName} sets to run with BITS but it's not an executable file. BITS is not going to be used");
#endif
                                _installationPackage.RunWithBits = false;
                            }

                            if (_installationPackage.RunWithBits)
                            {
                                BitsEnabled = true;
                            }
                            else
                            {
                                p = new Process();
                                p.StartInfo.FileName        = runName;
                                p.StartInfo.Arguments       = runParam;
                                p.StartInfo.UseShellExecute = !FileUtils.IsRunnableFile(runName);
                            }

                            if (Path.GetExtension(runName).ToLower() == ".msi")
                            {
                                int msiTimeout = _installationPackage.msiTimeout;
                                if (!WaitForMsi(runName, msiTimeout))
                                {
                                    throw new Win32Exception(ERROR_INSTALL_ALREADY_RUNNING);
                                }
                            }

                            _installationPackage.HandleRunStart();

                            Process[] preRunningProcesses = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(runName));
                            int preRunningProcessesCount  = preRunningProcesses.Length;
                            foreach (Process preProcess in preRunningProcesses)
                            {
                                preProcess.EnableRaisingEvents = true;
                                preProcess.Exited += new EventHandler(delegate(object sender, EventArgs e)
                                {
                                    preRunningProcessesCount--;
                                });
                            }

                            //release the JobTransferred lock
                            if (_installationPackage.onRunWithBits.Set() && BitsEnabled)
                            {
                                Process[] processByName = new Process[] { };
                                Stopwatch timer         = new Stopwatch();
                                timer.Start();
                                do
                                {
                                    processByName = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(runName));
                                    Thread.Sleep(100);
                                } while (processByName.Length <= preRunningProcessesCount && timer.Elapsed.TotalSeconds < 10);
                                timer.Stop();

                                if (processByName.Length > preRunningProcessesCount)
                                {
                                    foreach (Process candidate in processByName)
                                    {
                                        if (preRunningProcesses.All(current => current.Id != candidate.Id) &&
                                            candidate.ProcessName != candidate.Parent().ProcessName)
                                        {
                                            p = candidate;
                                            break;
                                        }
                                    }
                                }
                            }

                            if (p != null)
                            {
                                p.EnableRaisingEvents = true;
                                p.Exited += (object sender, EventArgs e) =>
                                {
                                    if (sender is Process process)
                                    {
                                        _installationPackage.runExitCode = process.ExitCode;
                                        if (_installationPackage.waitForIt)
                                        {
                                            _installationPackage.HandleRunEnd();
                                        }
                                    }
                                };

                                if (!BitsEnabled)
                                {
                                    p.Start();
                                }
                            }
                            else
                            {
                                throw new Exception($"Process not found: {Path.GetFileName(runName)}");
                            }
                        }
                        catch (Win32Exception ex)
                        {
                            _installationPackage.runErrorCode = ex.NativeErrorCode;
                            throw;
                        }
                        finally
                        {
                            BitsEnabled = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        _installationPackage.ErrorMessage = $"Running {runName} {runParam} - {ex.Message}";
                        _installationPackage.OnInstallFailed(ErrorConsts.ERR_RUN_GENERAL, _installationPackage.ErrorMessage);
                    }

                    if (!_installationPackage.waitForIt)
                    {
                        _installationPackage.HandleRunEnd();
                    }
                }
            });
            _runnerThread.Start();
        }