Ejemplo n.º 1
0
        private void SleepIfNecessary()
        {
            SleepControls.AllowSleepOrShutdown(Handle);

            if (checkBoxSleep.Checked)
            {
                if (!SleepControls.PutToSleep())
                {
                    MessageBox.Show("Sleep when done", "Could not put the PC to sleep for some reason :(",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

            checkBoxSleep.Enabled = false;
        }
        private UninstallProgressWindow()
        {
            InitializeComponent();

            Text += " - Bulk Crap Uninstaller";

            Opacity = 0;

            Icon = Resources.Icon_Logo;

            toolStrip1.Renderer = new ToolStripProfessionalRenderer(new StandardSystemColorTable());

            // Shutdown blocking not available below Windows Vista
            if (Environment.OSVersion.Version >= new Version(6, 0))
            {
                _settings.Subscribe((sender, args) =>
                {
                    if (args.NewValue)
                    {
                        SleepControls.PreventSleepOrShutdown(Handle, "Bulk uninstallation is in progress.");
                    }
                    else
                    {
                        SleepControls.AllowSleepOrShutdown(Handle);
                    }
                }, settings => settings.UninstallPreventShutdown, this);
            }

            _settings.SendUpdates(this);

            FormClosing += (sender, args) =>
            {
                if (args.CloseReason == CloseReason.WindowsShutDown && _settings.Settings.UninstallPreventShutdown)
                {
                    args.Cancel = true;
                }
            };

            FormClosed += (o, eventArgs) =>
            {
                _settings.RemoveHandlers(this);

                SleepControls.AllowSleepOrShutdown(Handle);

                _walkAwayBox?.Dispose();
            };

            olvColumnName.AspectGetter     = BulkUninstallTask.DisplayNameAspectGetter;
            olvColumnStatus.AspectGetter   = BulkUninstallTask.StatusAspectGetter;
            olvColumnIsSilent.AspectGetter = BulkUninstallTask.IsSilentAspectGetter;
            olvColumnId.AspectName         = nameof(BulkUninstallEntry.Id);

            olvColumnStatus.GroupKeyGetter = rowObject => (rowObject as BulkUninstallEntry)?.CurrentStatus;
            olvColumnName.GroupKeyGetter   = rowObject => (rowObject as BulkUninstallEntry)?.UninstallerEntry.DisplayName.SafeNormalize().FirstOrDefault();
            olvColumnId.GroupKeyGetter     = rowObject => (rowObject as BulkUninstallEntry)?.Id / 10;

            objectListView1.PrimarySortColumn   = olvColumnStatus;
            objectListView1.SecondarySortColumn = olvColumnId;

            forceUpdateTimer.Tick += (o, args) =>
            {
                if (_currentTargetStatus != null && !_currentTargetStatus.Finished)
                {
                    currentTargetStatus_OnCurrentTaskChanged(o, args);
                }
                else
                {
                    forceUpdateTimer.Stop();
                }
            };

            // Handle sleeping after task finishes
            sleepTimer.Interval = 1000;
            sleepTimer.Tick    += (sender, args) =>
            {
                if (_currentTargetStatus.Finished && checkBoxFinishSleep.Checked)
                {
                    _sleepTimePassed++;

                    const int sleepDelay = 30;
                    label1.Text = Localisable.UninstallProgressWindow_TaskDone + "\n" +
                                  string.Format(Localisable.UninstallProgressWindow_StatusPuttingToSleepInSeconds, sleepDelay - _sleepTimePassed);

                    if (_sleepTimePassed > sleepDelay)
                    {
                        checkBoxFinishSleep.Checked = false;
                        checkBoxFinishSleep.Enabled = false;

                        SleepControls.AllowSleepOrShutdown(Handle);
                        SleepControls.PutToSleep();
                    }
                }
                else
                {
                    _sleepTimePassed = 0;
                    if (_currentTargetStatus.Finished)
                    {
                        label1.Text = Localisable.UninstallProgressWindow_TaskDone;
                    }
                }
            };
        }