Exemple #1
0
        internal static ProgressTracker StartProgress(ProgressTracker parentTracker, string message, Request request)
        {
            if (request == null)
            {
                return null;
            }

            // if parent tracker is null, use 0 for parent id, else use the progressid of parent tracker
            return new ProgressTracker(request.StartProgress(parentTracker == null ? 0 : parentTracker.ProgressID, message));
        }
        private static bool ExecuteUninstallCommand(string fastPackageReference, Request request, string file, string args)
        {
            Timer timer = null;
            object timerLock = new object();
            bool cleanUp = false;

            ProgressTracker tracker = new ProgressTracker(request.StartProgress(0, Resources.Messages.Uninstalling));
            double percent = tracker.StartPercent;

            Action cleanUpAction = () => {
                lock (timerLock)
                {
                    // check whether clean up is already done before or not
                    if (!cleanUp)
                    {
                        try
                        {
                            if (timer != null)
                            {
                                // stop timer
                                timer.Change(Timeout.Infinite, Timeout.Infinite);
                                timer.Dispose();
                                timer = null;
                            }
                        }
                        catch
                        {
                        }

                        cleanUp = true;
                    }
                }
            };
            
            var start = new ProcessStartInfo
            {
                FileName = file,
                Arguments = args,
                UseShellExecute = false,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true,
                //LoadUserProfile = true,
            };

           
            using (var proc = Process.Start(start))
            {
                // percent between startProgress and endProgress
                var progressPercent = tracker.ConvertPercentToProgress(percent += 0.01);

                request.Progress(tracker.ProgressID, (int)progressPercent, Resources.Messages.RunningCommand, file);

                if (proc == null)
                {
                    return false;
                }
              
                timer = new Timer(_ => {
                    percent += 0.025;
                   
                    if (progressPercent < 90)
                    {
                        request.Progress(tracker.ProgressID, (int) progressPercent, Resources.Messages.RunningCommand, file);
                    }
                    if (request.IsCanceled)
                    {
                        cleanUpAction();
                    }
                }, null, 0, 3000);


                proc.WaitForExit();
                if (proc.ExitCode != 0)
                {
                    request.Error(ErrorCategory.InvalidOperation, fastPackageReference, Resources.Messages.UninstallFailed, file, proc.StandardError.ReadToEnd());
                    request.CompleteProgress(tracker.ProgressID, false);

                    return false;
                }
                request.CompleteProgress(tracker.ProgressID, true);
                return true;
            }
        }