using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Threading;

namespace UseBackgroundWorker
{
    public partial class frm99UseBackgroundWorker : Form
    {
        public frm99UseBackgroundWorker()
        {
            InitializeComponent();
        }

        // Use the BackgroundWorker to perform a long task.
        private void goButton_Click(object sender, EventArgs e)
        {
            if (goButton.Text == "Go")
            {
                // Start the process.
                statusLabel.Text = "Working...";
                goButton.Text = "Stop";
                percentCompleteProgressBar.Value = 0;
                percentCompleteProgressBar.Visible = true;

                // Start the BackgroundWorker.
                longTaskBackgroundWorker.RunWorkerAsync();
            }
            else
            {
                // Stop the process.
                longTaskBackgroundWorker.CancelAsync();
            }
        }

        // Perform the long task.
        private void longTaskBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // Spend 10 seconds doing nothing.
            for (int i = 1; i <= 10; i++)
            {
                // If we should stop, do so.
                if (longTaskBackgroundWorker.CancellationPending)
                {
                    // Indicate that the task was canceled.
                    e.Cancel = true;
                    break;
                }

                // Sleep.
                Thread.Sleep(1000);

                // Notify the UI thread of our progress.
                longTaskBackgroundWorker.ReportProgress(i * 10);
            }
        }

        // Update the progress bar.
        private void longTaskBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            percentCompleteProgressBar.Value = e.ProgressPercentage;
        }

        // The long task is done.
        private void longTaskBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled) statusLabel.Text = "Canceled";
            else statusLabel.Text = "Finished";

            goButton.Text = "Go";
            percentCompleteProgressBar.Visible = false;
        }

        // Display the current time.
        private void clockTimer_Tick(object sender, EventArgs e)
        {
            clockLabel.Text = DateTime.Now.ToString("T");
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.clockTimer = new System.Windows.Forms.Timer(this.components);
            this.statusLabel = new System.Windows.Forms.Label();
            this.clockLabel = new System.Windows.Forms.Label();
            this.percentCompleteProgressBar = new System.Windows.Forms.ProgressBar();
            this.longTaskBackgroundWorker = new System.ComponentModel.BackgroundWorker();
            this.goButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // clockTimer
            // 
            this.clockTimer.Enabled = true;
            this.clockTimer.Interval = 1000;
            this.clockTimer.Tick += new System.EventHandler(this.clockTimer_Tick);
            // 
            // statusLabel
            // 
            this.statusLabel.AutoSize = true;
            this.statusLabel.Location = new System.Drawing.Point(16, 77);
            this.statusLabel.Name = "statusLabel";
            this.statusLabel.Size = new System.Drawing.Size(0, 13);
            this.statusLabel.TabIndex = 31;
            // 
            // clockLabel
            // 
            this.clockLabel.AutoSize = true;
            this.clockLabel.Location = new System.Drawing.Point(8, 7);
            this.clockLabel.Name = "clockLabel";
            this.clockLabel.Size = new System.Drawing.Size(0, 13);
            this.clockLabel.TabIndex = 30;
            // 
            // percentCompleteProgressBar
            // 
            this.percentCompleteProgressBar.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.percentCompleteProgressBar.Location = new System.Drawing.Point(16, 44);
            this.percentCompleteProgressBar.Name = "percentCompleteProgressBar";
            this.percentCompleteProgressBar.Size = new System.Drawing.Size(329, 17);
            this.percentCompleteProgressBar.TabIndex = 29;
            this.percentCompleteProgressBar.Visible = false;
            // 
            // longTaskBackgroundWorker
            // 
            this.longTaskBackgroundWorker.WorkerReportsProgress = true;
            this.longTaskBackgroundWorker.WorkerSupportsCancellation = true;
            this.longTaskBackgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.longTaskBackgroundWorker_DoWork);
            this.longTaskBackgroundWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.longTaskBackgroundWorker_ProgressChanged);
            this.longTaskBackgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.longTaskBackgroundWorker_RunWorkerCompleted);
            // 
            // goButton
            // 
            this.goButton.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.goButton.Location = new System.Drawing.Point(143, 15);
            this.goButton.Name = "goButton";
            this.goButton.Size = new System.Drawing.Size(75, 23);
            this.goButton.TabIndex = 28;
            this.goButton.Text = "Go";
            this.goButton.UseVisualStyleBackColor = true;
            this.goButton.Click += new System.EventHandler(this.goButton_Click);
            // 
            // frm99UseBackgroundWorker
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(353, 96);
            this.Controls.Add(this.statusLabel);
            this.Controls.Add(this.clockLabel);
            this.Controls.Add(this.percentCompleteProgressBar);
            this.Controls.Add(this.goButton);
            this.Name = "frm99UseBackgroundWorker";
            this.Text = "UseBackgroundWorker";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Timer clockTimer;
        private System.Windows.Forms.Label statusLabel;
        private System.Windows.Forms.Label clockLabel;
        private System.Windows.Forms.ProgressBar percentCompleteProgressBar;
        private System.ComponentModel.BackgroundWorker longTaskBackgroundWorker;
        private System.Windows.Forms.Button goButton;
    }
}