forked from sergiisyrovatchenko/SQLIndexManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefreshOperation.cs
More file actions
115 lines (86 loc) · 3.11 KB
/
Copy pathRefreshOperation.cs
File metadata and controls
115 lines (86 loc) · 3.11 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SQLIndexManager.Common {
public class RefreshOperation : BackgroundWorker {
private List<string> databases;
private Host host;
private List<Index> indexes;
private bool canceled;
ThreadWorker currentWorker;
public RefreshOperation(Host host, List<string> databases) {
this.host = host;
this.databases = databases;
}
public RunWorkerCompletedEventHandler FinishScan;
public void Cancel() {
canceled = true;
currentWorker.Abort();
}
public void Start(ProgressBox progress) {
Queue<ThreadWorker> threadQueue = new Queue<ThreadWorker>();
for (int i = 0; i < databases.Count; i++) {
string database = databases[i];
ThreadWorker _worker = new ThreadWorker() { WorkerReportsProgress = true };
_worker.DoWork += StartScan;
_worker.ProgressChanged += progress.ProgressChanged;
threadQueue.Enqueue(_worker);
}
SqlConnection connection = Connection.Create(host, ConnectionType.DefaultDatabase);
foreach (var _worker in threadQueue) {
_worker.RunWorkerCompleted += progress.FinishScan;
if (!canceled)
try {
connection.Open();
currentWorker = _worker;
_worker.RunWorkerAsync();
}
finally {
connection.Close();
}
}
OnFinish();
}
private void StartScan(object sender, DoWorkEventArgs e) {
if (e.Cancel)
return;
BackgroundWorker worker = (BackgroundWorker)sender;
if (!Database.Check(connection, database)) {
worker.ReportProgress(0, $"Database {database} is not accessible.");
}
else {
worker.ReportProgress(0, $"Scan indexes {database}");
if (progress.InvokeRequired)
progress.Invoke(new Action(() => progress.Text = string.Format((databases.Count == 1 ? "{2}" : "{0} / {1} - {2}"), i + 1, databases.Count, database)));
if (e.Cancel)
return;
host.Database = database;
DataTable dt = QueryEngine.GetIndexes(host);
List<Index> idx = IndexInfoBuilder.GetIndexes(dt, host);
if (progress.InvokeRequired)
progress.Invoke(new Action(() => progress.Properties.Maximum = idx.Count));
int cnt = idx.Count - idx.Count(_ => _.Fragmentation == null);
if (e.Cancel)
return;
foreach (Index index in idx.Where(_ => _.Fragmentation == null)) {
worker.ReportProgress(cnt++, index.ToString());
QueryEngine.GetIndexFragmentation(index, host);
}
worker.ReportProgress(cnt, "Scan finished");
lock (indexes) {
indexes.AddRange(idx);
}
}
}
private void OnFinish() {
if (FinishScan != null)
FinishScan(this, new RunWorkerCompletedEventArgs(indexes, null, canceled));
}
}
}