kijato

c#_OperationInTheBackground.cs

Sep 26th, 2020
1,017
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6.  
  7. namespace BackgroundWorkerExample
  8. {
  9.  
  10.     public class Form1 : Form
  11.     {
  12.         BackgroundWorker backgroundWorker1 = new BackgroundWorker();
  13.         Button startBtn = new Button() { Location = new Point(10, 10), Size = new Size(75, 23), Text = "Start" };
  14.         Button cancelBtn = new Button() { Location = new Point(100, 10), Size = new Size(75, 23), Text = "Cancel" };
  15.         Label labelLbl = new Label() { Location = new Point(10, 60), Size = new Size(95, 23), Text = "-", Font = new Font("Verdana", 12) };
  16.  
  17.         [STAThread]
  18.         static void Main()
  19.         {
  20.             Application.EnableVisualStyles();
  21.             Application.Run(new Form1());
  22.         }
  23.  
  24.         public Form1()
  25.         {
  26.             backgroundWorker1.WorkerReportsProgress = true;
  27.             backgroundWorker1.WorkerSupportsCancellation = true;
  28.             backgroundWorker1.DoWork             += new DoWorkEventHandler(DoWork);
  29.             backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted);
  30.             backgroundWorker1.ProgressChanged    += new ProgressChangedEventHandler(ProgressChanged);
  31.  
  32.             startBtn.Click  += new System.EventHandler(startBtn_Click);
  33.             cancelBtn.Click += new System.EventHandler(cancelBtn_Click);
  34.  
  35.             ClientSize = new Size(190, 100);
  36.  
  37.             Controls.Add(cancelBtn);
  38.             Controls.Add(startBtn);
  39.             Controls.Add(labelLbl);
  40.         }
  41.  
  42.         void startBtn_Click(object sender, EventArgs e)
  43.         {
  44.             if ( backgroundWorker1.IsBusy != true )
  45.                 backgroundWorker1.RunWorkerAsync();
  46.             else
  47.                 MessageBox.Show("The worker is running...");
  48.         }
  49.  
  50.         void cancelBtn_Click(object sender, EventArgs e)
  51.         {
  52.             if ( backgroundWorker1.IsBusy == true && backgroundWorker1.WorkerSupportsCancellation == true )
  53.                 backgroundWorker1.CancelAsync();
  54.             else
  55.                 MessageBox.Show("There isn't worker...");
  56.         }
  57.  
  58.         void DoWork(object sender, DoWorkEventArgs e)
  59.         {
  60.             int counter = 50;
  61.             for (int i = 1; i <= counter; i++)
  62.             {
  63.                 BackgroundWorker worker = sender as BackgroundWorker;
  64.                 if (worker.CancellationPending == true)
  65.                 {
  66.                     e.Cancel = true;
  67.                     break;
  68.                 }
  69.                 else
  70.                 {
  71.                     System.Threading.Thread.Sleep(500);
  72.                     worker.ReportProgress(i * 100 / counter);
  73.                 }
  74.             }
  75.         }
  76.  
  77.         void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  78.         {
  79.             if (e.Cancelled == true)
  80.             {
  81.                 labelLbl.Text = "Canceled!";
  82.             }
  83.             else if (e.Error != null)
  84.             {
  85.                 labelLbl.Text = "Error: " + e.Error.Message;
  86.             }
  87.             else
  88.             {
  89.                 labelLbl.Text = "Done!";
  90.             }
  91.         }
  92.  
  93.         private void ProgressChanged(object sender, ProgressChangedEventArgs e)
  94.         {
  95.             labelLbl.Text = e.ProgressPercentage.ToString() + "%";
  96.         }
  97.  
  98.     }
  99.  
  100. }
RAW Paste Data