我最近为 Windows 服务检查器做了类似的事情。它最终也很容易实现。
在您的情况下,我认为需要以下内容。
文件 - 它的唯一目的是下载文件并通知更改。
FileManager - 维护文件列表并添加新的,删除等。
public class File : INotifyPropertyChanged
{
private readonly string _fileName;
private Thread _thread;
private Task _task;
private bool _cancelled;
private TaskStatus _taskStatus;
private int _taskProgress;
private int _taskTotal;
public event PropertyChangedEventHandler PropertyChanged;
public File(string fileName)
{
_fileName = fileName;
TaskStatus = TaskStatus.NotStarted;
}
public TaskStatus TaskStatus
{
get { return _taskStatus; }
private set
{
_taskStatus = value;
PropertyChanged.Raise(this, x => x.TaskStatus);
}
}
public int TaskProgress
{
get { return _taskProgress; }
private set
{
_taskProgress = value;
PropertyChanged.Raise(this, x => x.TaskProgress);
}
}
public int TaskTotal
{
get { return _taskTotal; }
private set
{
_taskTotal = value;
PropertyChanged.Raise(this, x => x.TaskTotal);
}
}
public void StartTask()
{
_cancelled = false;
//.Net 4 - task parallel library - nice
_task = new Task(DownloadFile, TaskCreationOptions.LongRunning);
_task.Start();
//.Net Other
_thread = new Thread(DownloadFile);
_thread.Start();
}
public void CancelTask()
{
_cancelled = true;
}
private void DownloadFile()
{
try
{
TaskStatus = TaskStatus.Running;
var fileLength = _fileName.Length;
TaskTotal = fileLength;
for (var i = 0; i < fileLength; i++)
{
if (_cancelled)
{
TaskStatus = TaskStatus.Cancelled;
return;
}
//Some work to download the file
Thread.Sleep(1000); //sleep for the example instead
TaskProgress = i;
}
TaskStatus = TaskStatus.Completed;
}
catch (Exception ex)
{
TaskStatus = TaskStatus.Error;
}
}
}
public enum TaskStatus
{
NotStarted,
Running,
Completed,
Cancelled,
Error
}
public static class NotifyPropertyChangedExtention
{
public static void Raise<T, TP>(this PropertyChangedEventHandler pc, T source, Expression<Func<T, TP>> pe)
{
if (pc != null)
{
pc.Invoke(source, new PropertyChangedEventArgs(((MemberExpression)pe.Body).Member.Name));
}
}
}
这样做的好处是您永远不需要从后台线程更新 UI。您正在更新的是只读属性,只有后台类也会写入。此类之外的任何内容都只能读取,因此您不必担心锁定。当引发 PropertyChanged 时,UI 绑定系统将收到属性已更改的通知,然后将读取该值。
现在是经理
public class FileManager
{
public ObservableCollection<File> ListOfFiles { get; set; }
public void AddFile(string fileName)
{
var file = new File(fileName);
file.PropertyChanged += FilePropertyChanged;
file.StartTask();
ListOfFiles.Add(file);
}
void FilePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "TaskStatus")
{
var file = (File) sender;
if (file.TaskStatus==TaskStatus.Completed)
{
RemoveFile(file);// ??? automatically remove file from list on completion??
}
}
}
public void RemoveFile(File file)
{
if (file.TaskStatus == TaskStatus.Running)
{
file.CancelTask();
}
//unbind event
file.PropertyChanged -= FilePropertyChanged;
ListOfFiles.Remove(file);
}
}
现在您需要在视图模型中做的就是从 FileManager 公开 ListOfFiles,这是一个可观察的集合。来自它的通知将让绑定系统知道 UI 何时需要更新。
只需将 ListOfFiles 绑定到 ListView 或类似的,为 File 类添加一个数据模板,这将使列表视图知道如何呈现每个文件。
您的 WCF 服务器和视图模型应该具有对相同文件管理器的引用,WCF 添加和删除文件,视图模型使 ListOfFiles 可用于 UI。
这只是一个粗略的技巧来理解这个概念。您需要添加您认为合适的内容。
如果这有帮助,请告诉我。