Advertisement
ivandrofly

BackgroundWorker - C#

Feb 22nd, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Threading;
  4.  
  5. namespace Background_Worker
  6. {
  7.     class Program
  8.     {
  9.         static BackgroundWorker worker = new BackgroundWorker();
  10.         static void Main()
  11.         {
  12.             // these methods will be called and run work asynchronous
  13.             worker.DoWork += DoSomething;
  14.             worker.DoWork += DoBlaBla;
  15.  
  16.             worker.RunWorkerCompleted += (sender, e) =>
  17.             {
  18.                 Console.ForegroundColor = ConsoleColor.Green;
  19.                 Console.WriteLine("Work complreted");
  20.                 //Console.ReadLine(); // Wait here!
  21.             };
  22.             worker.ProgressChanged += (object sender, ProgressChangedEventArgs e) =>
  23.             {
  24.                 Console.WriteLine(e.ProgressPercentage);
  25.             };
  26.             worker.WorkerReportsProgress = true;
  27.             worker.RunWorkerAsync("Bla bla bla"); // call method DoSomething then BoBlabla aynchronous
  28.             Console.ReadLine();
  29.         }
  30.  
  31.         static void DoSomething(object sender, DoWorkEventArgs e)
  32.         {
  33.             BackgroundWorker obj = sender as BackgroundWorker;
  34.             Console.WriteLine("=================================================");
  35.             Console.WriteLine("Do something: " + e.Argument);
  36.             Console.WriteLine("Sent from: " + sender);
  37.             Console.WriteLine("=================================================");
  38.             obj.ReportProgress(50, "Completed");
  39.         }
  40.         static void DoBlaBla(object sender, DoWorkEventArgs e)
  41.         {
  42.             Console.WriteLine();
  43.             Console.WriteLine("=================================================");
  44.             Console.WriteLine("Bla bla bla... " + e.Argument);
  45.             Console.WriteLine("Sent from: " + sender);
  46.             Console.WriteLine("=================================================");
  47.             worker.ReportProgress(100, "Progress completed successfully!");
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement