-
Notifications
You must be signed in to change notification settings - Fork 8
/
BackgroundWorkerSample.cs
51 lines (46 loc) · 1.95 KB
/
BackgroundWorkerSample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.ComponentModel;
using System.Threading;
namespace CodeSamples.MultiThreading
{
public class BackgroundWorkerSample
{
private readonly AutoResetEvent resetEvent = new AutoResetEvent(false);
public void Go()
{
BackgroundWorker backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork);
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.WorkerSupportsCancellation = true;
//start background worker
if(!backgroundWorker.IsBusy)
{
backgroundWorker.RunWorkerAsync();
}
//wait for background worker to finish; not necessary, but required for testing samples, so they execute in order
resetEvent.WaitOne();
}
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Console.WriteLine($"...reporting progress: {e.ProgressPercentage}%");
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = (BackgroundWorker)sender;
for(int i=0;i<11;i++)
{
Console.WriteLine($"...doing it's job");
worker.ReportProgress(i*10);
System.Threading.Thread.Sleep(50);
}
e.Result = 42; //object, can use any class
}
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine($"...done, Result = {e.Result.ToString()}");
resetEvent.Set();
}
}
}