更新时间:2023-01-13 08:05:30
要从BackgroundWorker发送进度报告,请在DoWork
方法内使用ReportProgress()
.
For sending a progress report out from a BackgroundWorker you use ReportProgress()
inside your DoWork
method.
void worker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker theWorker = (BackgroundWorker)sender;
theWorker.ReportProgress(0, "just starting");
BackupParams bp = (BackupParams)e.Argument;
...
这将触发您的worker_ProgressChanged
方法,因此您可以从此处将报告放入控件中.
This then fires off your worker_ProgressChanged
method, so you can take the report from there into your controls.
诀窍在于,您必须创建另一个函数来处理zip创建过程中的进度更改.您无法在此处访问您的UI控件,因为它们位于不同的线程上.您应该可以为此创建一个lambda(而且我不知道确切的参数,如果我输入错了,请修复)
The trick is that you have to make another function to handle the progress change with your zip creation. You can't access your UI controls here because they are on a different thread. You should be able to create a lambda for this (and I don't know the exact parameters, please fix if I'm wrong)
zf.SaveProgress += (sender, eventArgs) =>
{
// Check if EvenType equals Saving_AfterWriteEntry or NullReferenceException will be thrown
if (eventArgs.EventType == ProgressEventType.Saving_AfterWriteEntry)
{
theWorker.ReportProgress(eventArgs.EntriesSaved, "Saving "+ eventArgs.CurrentEntry.FileName);
}
};
zf.AddProgress += (sender, eventArgs) =>
{
// Check if EventType equals Adding_afterAddEntry or NullReferenceException will be thrown
if (eventArgs.EventType == ZipProgressEventType.Adding_afterAddEntry)
{
theWorker.ReportProgress(eventArgs.EntriesAdded, "Adding "+ eventArgs.CurrentEntry.FileName);
}
};