-
Notifications
You must be signed in to change notification settings - Fork 0
/
WatchDogBroker.cs
93 lines (74 loc) · 2.88 KB
/
WatchDogBroker.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// ---------------------------------------------------------------
// Copyright (c) Raúl Lorenzo Boullosa
// Licensed under the MIT License.
// See License.txt in the project root for license information.
// ---------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Threading;
using TheWatchDog.Models;
namespace TheWatchDog.Brokers.WatchDogs
{
public class WatchDogBroker : IWatchDogBroker
{
private BackgroundWorker backgroundWorker;
private Action<WatchDog> actionOnRunHandler;
private Action<WatchDog, int, object> actionOnProgressHandler;
private Action<WatchDog, bool, object, Exception> actionOnCompleteHandler;
private WatchDog watchDog;
public WatchDogBroker() { }
private void InitializeBackgroundWorker()
{
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += BackgroundWorker_DoWork;
backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.WorkerSupportsCancellation = true;
}
private void DisposeBackgroundWorker()
{
backgroundWorker.DoWork -= BackgroundWorker_DoWork;
backgroundWorker.RunWorkerCompleted -= BackgroundWorker_RunWorkerCompleted;
backgroundWorker.ProgressChanged -= BackgroundWorker_ProgressChanged;
backgroundWorker.Dispose();
backgroundWorker = null;
}
private void BuildWatchDog()
{
this.watchDog = new WatchDog()
{
Id = Guid.NewGuid()
, Status = WatchDogStatus.Initializating
, IsBusy = () =>
backgroundWorker.IsBusy
, IsRequestedForCancellation = () =>
backgroundWorker.CancellationPending
, NotifyProgress = (int progressPorcentage, object userState) =>
backgroundWorker.ReportProgress(progressPorcentage, userState)
};
}
public void RunAndListen(Action<WatchDog> actionOnRunHandler
, Action<WatchDog, int, object> actionOnProgressHandler
, Action<WatchDog, bool, object, Exception> actionOnCompleteHandler)
{
this.actionOnRunHandler = actionOnRunHandler;
this.actionOnProgressHandler = actionOnProgressHandler;
this.actionOnCompleteHandler = actionOnCompleteHandler;
InitializeBackgroundWorker();
BuildWatchDog();
backgroundWorker.RunWorkerAsync();
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) =>
actionOnRunHandler(watchDog);
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) =>
actionOnProgressHandler(watchDog, e.ProgressPercentage, e.UserState);
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
actionOnCompleteHandler(watchDog, e.Cancelled, e.Result, e.Error);
DisposeBackgroundWorker();
}
public void Cancel() =>
backgroundWorker.CancelAsync();
}
}