c# - 如何将 Zip 文件创建进度异步报告到状态栏?
问题描述
我正在尝试在创建 zip 文件时向状态栏报告进度。
为了报告创建进度,我使用了以下指南:
ZipFile Creation Report Progress
我的状态栏目前使用 2 个类进行更新:
- 一个
INotifyPropertyChanged
叫做的类StatusBar
- 一个单独的类称为
Status
.
StatusBar
班级:
public class StatusBar : INotifyPropertyChanged
{
private string _message;
private string _submessage;
private bool _isindeterminate;
private bool _visible;
private int _min;
private int _max;
private double _progress;
public event PropertyChangedEventHandler PropertyChanged;
public StatusBar() { }
public int Min
{
get { return this._min; }
set
{
this._min = value;
this.OnPropertyChanged("Min");
}
}
public int Max
{
get { return this._max; }
set
{
this._max = value;
this.OnPropertyChanged("Max");
}
}
public double Progress
{
get { return this._progress; }
set
{
this._progress = value;
this.OnPropertyChanged("Progress");
}
}
public string Message
{
get { return this._message; }
set
{
this._message = value;
this.OnPropertyChanged("Message");
}
}
public string SubMessage
{
get { return this._submessage; }
set
{
this._submessage = value;
this.OnPropertyChanged("SubMessage");
}
}
public bool IsIndeterminate
{
get { return this._isindeterminate; }
set
{
this._isindeterminate = value;
this.OnPropertyChanged("IsIndeterminate");
}
}
public bool Visible
{
get { return this._visible; }
set
{
this._visible = value;
this.OnPropertyChanged("Visible");
}
}
void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Status
班级 :
public class Status
{
public static void Initialise(StatusBar status, string _message, string _submessage, int _min, int _max, double _progress, bool _visible, bool _isindeterminate)
{
status.Visible = _visible;
if (_isindeterminate == false)
{
status.Message = _message;
status.SubMessage = _submessage;
status.Progress = _progress;
status.Min = _min;
status.Max = _max;
status.IsIndeterminate = _isindeterminate;
}
else
{
status.Message = _message;
status.IsIndeterminate = _isindeterminate;
}
}
public static void UpdateProgress(StatusBar status, double _progress)
{
if (_progress == status.Max)
{
Complete(status);
}
else
{
status.Progress = _progress;
}
}
public static void UpdateMessage(StatusBar status, string _message)
{
status.Message = _message;
}
public static void UpdateSubMessage(StatusBar status, string _submessage)
{
status.SubMessage = _submessage;
}
public static void UpdateProgressMessage(StatusBar status, string _message, double _progress)
{
status.Message = _message;
status.Progress = _progress;
}
public static void Complete(StatusBar status)
{
status.Message = "Ready";
status.SubMessage = " ";
status.Min = 0;
status.Max = 0;
status.Progress = 0;
status.Visible = false;
status.IsIndeterminate = false;
}
}
为了创建 zip 文件和更新状态栏,我目前有以下内容:
string sourceDirectory = "C:\\Access\\Test";
string archive = @"C:\Access\Test.zip";
Status.Initialise(statusbar, "Creating Zip File...", "", 0, 100, 0, true, false);
Backup.CreateFromDirectory(sourceDirectory, archive,
new BasicProgress<double>(p => Status.UpdateProgressMessage(statusbar, $"{p:P2} archiving complete", p)));
上面的代码正在运行,并创建了 Zip 文件。
但是,仅在过程完成后才报告进度,而不是在过程中。
如何更改此设置以更新 zip 文件创建过程中的进度?
解决方案
推荐阅读
- bash - 在匹配字典中包含的值的模式后检索整个字典的脚本
- database - PowerBI (DAX) - Countif 包含列值(千行)
- javascript - 我想在 id=twork 中显示添加文本框 id=ttlpw 和 id=tplpw。但它不起作用
- java - 在没有运行 JBoss 实例的情况下使用 Weld 模拟类
- android - 如何将 java 客户端连接到在 android 中运行的 gRPC 服务器?
- .net - 如何使用已经转义的路径段指定新的 Uri 实例?
- stripe-payments - 更新订阅时的 SCA 身份验证
- optimization - 如何有效地优化折叠方法中的其他指标?
- java - 如何在 TestRunner 中从我的 Hooks 类中调用 @Before 方法?
- python - 如何让此代码对上面给出的名称进行计数和排序?(初学者)