/// <summary>Create a progress form for external control via the UpdateProgress method</summary> public ProgressForm(string title, string desc, Icon icon, ProgressBarStyle style) { using (this.SuspendLayout(true)) { // Note: No point in setting DialogResult, ShowDialog resets it to None AutoScaleMode = AutoScaleMode.Font; AutoScaleDimensions = new SizeF(6F, 13F); AutoSizeMode = AutoSizeMode.GrowOnly; StartPosition = FormStartPosition.CenterParent; FormBorderStyle = FormBorderStyle.FixedDialog; ClientSize = new Size(136, 20); HideOnClose = false; m_progress = new TextProgressBar { Style = style, Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top, MarqueeAnimationSpeed = 30, }; m_description = new Label { Text = desc ?? string.Empty, AutoSize = false, Anchor = AnchorStyles.Top | AnchorStyles.Left }; m_btn_cancel = new Button { Text = "Cancel", UseVisualStyleBackColor = true, TabIndex = 1, Anchor = AnchorStyles.Bottom | AnchorStyles.Right }; m_btn_cancel.Click += (s, a) => { if (CancelSignal != null) { CancelSignal.Set(); } m_btn_cancel.Text = "Cancelling..."; m_btn_cancel.Enabled = false; Enabled = false; }; Text = title ?? string.Empty; if (icon != null) { Icon = icon; } Controls.Add(m_description); Controls.Add(m_progress); Controls.Add(m_btn_cancel); } }
protected override void OnFormClosing(FormClosingEventArgs e) { // If cancel isn't allowed, ignore the Red X if (e.CloseReason == CloseReason.UserClosing && !AllowCancel) { e.Cancel = true; return; } // If the dialog was cancelled, ensure the 'CancelPending' flag is set if (DialogResult == DialogResult.Cancel && CancelSignal != null) { CancelSignal.Set(); } // Set the dialog result based on the state of 'CancelSignal'. DialogResult = CancelPending ? DialogResult.Cancel : DialogResult.OK; base.OnFormClosing(e); // If the form is hide on close and the user is closing, then just // hide the UI. The background thread is not automatically cancelled. if (HideOnClose && e.CloseReason == CloseReason.UserClosing) { Hide(); e.Cancel = true; Owner?.Focus(); return; } // If the form will be disposed and there is a Cancel event, automatically // signal Cancel as a convenience for terminating the worker thread. // Worker threads that don't test for 'CancelPending' will have to // prevent the form from closing until finished. if (CancelSignal != null) { CancelSignal.Set(); } }