Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Threading;
- namespace Background_Worker
- {
- class Program
- {
- static BackgroundWorker worker = new BackgroundWorker();
- static void Main()
- {
- // these methods will be called and run work asynchronous
- worker.DoWork += DoSomething;
- worker.DoWork += DoBlaBla;
- worker.RunWorkerCompleted += (sender, e) =>
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Work complreted");
- //Console.ReadLine(); // Wait here!
- };
- worker.ProgressChanged += (object sender, ProgressChangedEventArgs e) =>
- {
- Console.WriteLine(e.ProgressPercentage);
- };
- worker.WorkerReportsProgress = true;
- worker.RunWorkerAsync("Bla bla bla"); // call method DoSomething then BoBlabla aynchronous
- Console.ReadLine();
- }
- static void DoSomething(object sender, DoWorkEventArgs e)
- {
- BackgroundWorker obj = sender as BackgroundWorker;
- Console.WriteLine("=================================================");
- Console.WriteLine("Do something: " + e.Argument);
- Console.WriteLine("Sent from: " + sender);
- Console.WriteLine("=================================================");
- obj.ReportProgress(50, "Completed");
- }
- static void DoBlaBla(object sender, DoWorkEventArgs e)
- {
- Console.WriteLine();
- Console.WriteLine("=================================================");
- Console.WriteLine("Bla bla bla... " + e.Argument);
- Console.WriteLine("Sent from: " + sender);
- Console.WriteLine("=================================================");
- worker.ReportProgress(100, "Progress completed successfully!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement