导出文件时更新进度条
Update progressbar while export files
我目前正在进行一个项目。它是一个 C# WinForms project.Basically,使用应该加载一个 file.dat 并处理该文件以生成一些文件的其他格式导出。当导出函数为 运行 时我需要更新进度条时问题就开始了,因为方法导出在其他程序集的其他 class 中,因为我将项目分成 3,我有一个 dll这是控制台应用程序和图形应用程序的常用方法,因此我无法更改从 dll 导出的方法,因为控制台应用程序依赖于该实现。
我有一个允许打开文件并单击导出按钮的表单,当我单击导出时,创建一个新表单并且在导出方法有效时进度条开始更新。在导出方法中,我无法添加更多参数,因为其他class 取决于初始实现。
所以我需要知道如何在方法中处理文件时更新进度条,每个输入文件生成超过 1 个文件。
我的导出函数是:
这是来自一个组件的 class 中的一个:
public static void Export(File file, string output, string inputFile,BackgroundWorker worker)
{
//Other operations
//......
//
for (int m = 0; m < variable.Count; m++)
{
ExportoFile(varibel, output);
worker.ReportProgress(m + 1);
}
worker.CancelAsync();
}
如你所见,现在我有了它,程序就这样运行了。
这是另一个来自其他程序集的 class(表格):
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
Exporter.Export(file, output, inputFile, worker);
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// The progress percentage is a property of e
progressBar.Value = e.ProgressPercentage;
progressLabel.Text = ((100 * (e.ProgressPercentage))/size ).ToString() +"%";
if (e.ProgressPercentage == size)
{
worker.CancelAsync();
this.Close();
}
}
我从我的表单创建了一个后台工作程序,它在我导出一个文件时更新进度。但是我需要另一种方法来处理它,因为我的导出方法是错误的。我不必发送工人作为参数,因为原始方法没有,其他 class 取决于原始实现。
总而言之,如何在不修改我的导出方法的情况下从其他程序集中的其他 class 导出文件时更新进度条?
您不应通过参数将后台工作程序发送到您的导出方法。在我看来,您可以使用一个简单的事件。
您需要使用一个新事件来扩展您的导出器 class。因为这个事件是新的,所以你的依赖应该不是问题。
所以你的出口商 class 看起来……类似这样:
public class ProgressEventArgs : EventArgs
{
public int Percentage {get;set}
}
public class Exporter
{
public static event EventHandler<ProgressEventArgs> ReportProgress
public void Export(File file, string output, string inputFile)
{
for (int m = 0; m < variable.Count; m++)
{
ExportoFile(varibel, output);
ReportProgress(this, new ProgressEventArgs {Percentage = m + 1};
}
}
}
您的 DoWork 将这样实现:
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
Exporter.ReportProgress += reportProgress; //Make sure you call this once
Exporter.Export(file, output, inputFile, worker);
}
private void reportProgress(object sender, ProgressEventArgs e)
{
//use e.Percentage
}
此外,如果您使用重载向您的方法添加新参数,应该不会有问题。所以你的方法仍然有效。但是使用事件方法要干净得多。
我目前正在进行一个项目。它是一个 C# WinForms project.Basically,使用应该加载一个 file.dat 并处理该文件以生成一些文件的其他格式导出。当导出函数为 运行 时我需要更新进度条时问题就开始了,因为方法导出在其他程序集的其他 class 中,因为我将项目分成 3,我有一个 dll这是控制台应用程序和图形应用程序的常用方法,因此我无法更改从 dll 导出的方法,因为控制台应用程序依赖于该实现。
我有一个允许打开文件并单击导出按钮的表单,当我单击导出时,创建一个新表单并且在导出方法有效时进度条开始更新。在导出方法中,我无法添加更多参数,因为其他class 取决于初始实现。
所以我需要知道如何在方法中处理文件时更新进度条,每个输入文件生成超过 1 个文件。
我的导出函数是:
这是来自一个组件的 class 中的一个:
public static void Export(File file, string output, string inputFile,BackgroundWorker worker)
{
//Other operations
//......
//
for (int m = 0; m < variable.Count; m++)
{
ExportoFile(varibel, output);
worker.ReportProgress(m + 1);
}
worker.CancelAsync();
}
如你所见,现在我有了它,程序就这样运行了。
这是另一个来自其他程序集的 class(表格):
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
Exporter.Export(file, output, inputFile, worker);
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// The progress percentage is a property of e
progressBar.Value = e.ProgressPercentage;
progressLabel.Text = ((100 * (e.ProgressPercentage))/size ).ToString() +"%";
if (e.ProgressPercentage == size)
{
worker.CancelAsync();
this.Close();
}
}
我从我的表单创建了一个后台工作程序,它在我导出一个文件时更新进度。但是我需要另一种方法来处理它,因为我的导出方法是错误的。我不必发送工人作为参数,因为原始方法没有,其他 class 取决于原始实现。
总而言之,如何在不修改我的导出方法的情况下从其他程序集中的其他 class 导出文件时更新进度条?
您不应通过参数将后台工作程序发送到您的导出方法。在我看来,您可以使用一个简单的事件。
您需要使用一个新事件来扩展您的导出器 class。因为这个事件是新的,所以你的依赖应该不是问题。
所以你的出口商 class 看起来……类似这样:
public class ProgressEventArgs : EventArgs
{
public int Percentage {get;set}
}
public class Exporter
{
public static event EventHandler<ProgressEventArgs> ReportProgress
public void Export(File file, string output, string inputFile)
{
for (int m = 0; m < variable.Count; m++)
{
ExportoFile(varibel, output);
ReportProgress(this, new ProgressEventArgs {Percentage = m + 1};
}
}
}
您的 DoWork 将这样实现:
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
Exporter.ReportProgress += reportProgress; //Make sure you call this once
Exporter.Export(file, output, inputFile, worker);
}
private void reportProgress(object sender, ProgressEventArgs e)
{
//use e.Percentage
}
此外,如果您使用重载向您的方法添加新参数,应该不会有问题。所以你的方法仍然有效。但是使用事件方法要干净得多。