-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkspace.cs
279 lines (239 loc) · 8.98 KB
/
Workspace.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System;
using System.Xml.Serialization;
namespace AutoUpdate {
/* xml 的示例:
* <Workspace>
* <SourcePath>c:\abc\</SourcePath>
* <OnlyLastHour>true</OnlyLastHour>
* </Workspace>
*/
public class Workspace : INotifyPropertyChanged,IDisposable {
private string _sourcePath;
/// <summary>
/// 源路径
/// </summary>
public string SourcePath {
get{return _sourcePath;}
set {
if (_sourcePath != value) {
_sourcePath = value;
this.OnPropertyChanged("SourcePath");
this.Dirty = true;
_sourceFilesManager.Start(value);
HookSourceFiles(); //跟踪源路径的变化,目标路径无需跟踪。
}
}
}
private void HookSourceFiles() {
if (_watcher == null) {
_watcher = new AssemblyWatcher();
_watcher.Changed += OnSourceFilesChanged;
}
_watcher.Path = _sourcePath;
}
private void OnSourceFilesChanged(object sender, EventArgs e) {
_sourceFilesManager.Start(_sourcePath);
}
private AssemblyWatcher _watcher;
private TaskManager<string,SourceFileCollection> _sourceFilesManager = new TaskManager<string,SourceFileCollection>(SourceFileCollection.DoWork);
private string _targetPath;
/// <summary>
/// 目标路径
/// </summary>
[XmlIgnore]
public string TargetPath {
get { return _targetPath; }
set {
if (_targetPath != value) {
_targetPath = value;
this.OnPropertyChanged("TargetPath");
_targetFilesManager.Start(value);
}
}
}
private TaskManager<string,TargetFileCollection> _targetFilesManager = new TaskManager<string,TargetFileCollection>(TargetFileCollection.DoWork);
private bool _onlyLastHour = true;
/// <summary>
/// 仅同步最后一小时更新的组件。
/// </summary>
public bool OnlyLastHour {
get { return _onlyLastHour; }
set {
if (_onlyLastHour != value) {
_onlyLastHour = value;
this.OnPropertyChanged("OnlyLastHour");
this.Dirty = true;
}
}
}
public void Sync() {
_syncTaskManager.Start(this);
}
public void CancelAll() {
_syncTaskManager.Cancel();
_sourceFilesManager.Cancel();
_targetFilesManager.Cancel();
}
private TaskManager<Workspace, bool> _syncTaskManager = new TaskManager<Workspace, bool>(DoWork);
private bool _working;
[XmlIgnore]
public bool Working {
get { return _working; }
private set {
if (_working != value) {
_working = value;
OnPropertyChanged("Working");
OnPropertyChanged("CanSync");
}
}
}
[XmlIgnore]
public bool CanSync {
get { return !_working; }
}
//为后台任务准备。
internal static void DoWork(DoWorkEventArgs e) {
var owner = (Workspace)e.Argument;
owner.Working = true;
try {
owner.SyncCore(e);
}
finally {
owner.CompleteCount = 0;
owner.Working = false;
}
}
private void SyncCore(CancelEventArgs e) {
AddMessage("获取源目录清单: " + _sourcePath);
var sourceFiles = _sourceFilesManager.Result;
var sourcePath = _sourceFilesManager.LastArgs;
AddMessage("文件数: " + (sourceFiles == null ? 0 : sourceFiles.Count));
AddMessage("获取目标目录清单: " + _sourcePath);
var targetFiles = _targetFilesManager.Result;
var targetPath = _targetFilesManager.LastArgs;
AddMessage("文件数: " + (targetFiles == null ? 0 : targetFiles.Count));
if (sourceFiles == null || sourceFiles.IsErrorStatus ||
targetFiles == null || targetFiles.IsErrorStatus ||
!object.ReferenceEquals(sourcePath, _sourcePath) ||
!object.ReferenceEquals(targetPath, _targetPath)) {
AddMessage("失败,路径不准确或当前状态无效。");
return;
}
//找到源中需要更新的文件,
if (OnlyLastHour) {
sourceFiles = sourceFiles.GetLastHourItems();
}
List<KeyValuePair<FileInfo, FileInfo>> lst = new List<KeyValuePair<FileInfo, FileInfo>>();
foreach (var sourceFile in sourceFiles) {
IEnumerable<FileInfo> targetList;
if (e.Cancel) {
throw new OperationCanceledException();
}
if (targetFiles.TryGet(sourceFile.Name, out targetList)) {
foreach (var targetFile in targetList) {
if (sourceFile.LastWriteTime > targetFile.LastWriteTime) {
lst.Add(new KeyValuePair<FileInfo, FileInfo>(sourceFile, targetFile));
}
}
}
}
this.SyncFileCount = lst.Count - 1;
int completeCount = 0;
foreach (var item in lst) {
if (e.Cancel) {
throw new OperationCanceledException();
}
try {
File.Copy(item.Key.FilePath, item.Value.FilePath, true);
item.Value.UpdateLastWriteTime();
//System.Threading.Thread.Sleep(500);
this.AddMessage("更新: " + item.Value.FilePath);
completeCount++;
}
catch {
this.AddMessage("失败: " + item.Value.FilePath);
}
this.CompleteCount++;
}
this.AddMessage(string.Format(@"========== 已结束,成功数 / 总数: {0} / {1} ==========", completeCount, lst.Count));
}
private void AddMessage(string message) {
if (this.MessageAdded != null) {
var time = DateTime.Now.ToLongTimeString();
this.MessageAdded(this,new MessageAddedEventArgs(time + " " + message));
}
}
internal event EventHandler<MessageAddedEventArgs> MessageAdded;
private int _syncFileCount = 100;
[XmlIgnore]
public int SyncFileCount {
get { return _syncFileCount; }
set {
if (_syncFileCount != value && value > 0) {
_syncFileCount = value;
this.OnPropertyChanged("SyncFileCount");
}
}
}
private int _completeCount;
[XmlIgnore]
public int CompleteCount {
get { return _completeCount; }
set {
if (_completeCount != value) {
_completeCount = value;
this.OnPropertyChanged("CompleteCount");
}
}
}
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) {
if (this.PropertyChanged != null) {
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private bool _dirty;
/// <summary>
/// 返回当前对象是否已经发生修改。
/// </summary>
[XmlIgnore]
public bool Dirty {
get { return _dirty; }
internal set {
if (_dirty != value) {
_dirty = value;
this.OnPropertyChanged("Dirty");
}
}
}
#endregion
#region IDisposable Support
private bool disposedValue = false; // 要检测冗余调用
protected virtual void Dispose(bool disposing) {
if (!disposedValue) {
if (disposing) {
CancelAll();
}
if (_watcher != null) {
_watcher.Dispose();
_watcher = null;
}
disposedValue = true;
}
}
public void Dispose() {
Dispose(true);
}
#endregion
}
class MessageAddedEventArgs : EventArgs {
public MessageAddedEventArgs(string message) {
this.Message = message;
}
public string Message { get; private set; }
}
}