简体   繁体   English

C#Backgroundworker报告2个进度条的进度?

[英]C# Backgroundworker report progress for 2 progress bars?

I have several large files that I'm processing and I'm using the backgroundWorker1.ReportProgress(percent) to report the progress as each file is finished. 我正在处理几个大文件,并且我使用backgroundWorker1.ReportProgress(percent)来报告每个文件完成时的进度。 But now I want to have a second progress bar to report the progress for each individual file as it's being processed (and resets for the next file). 但现在我想要第二个进度条来报告每个文件正在处理时的进度(并重置下一个文件)。 I can use the current byte position I'm reading from and divide by the total size of the file to get the progress in percentage. 我可以使用我正在读取的当前字节位置并除以文件的总大小来获得百分比的进度。 But how do I pass this value to progressBar2 if there's only one ProgressChanged event? 但是,如果只有一个ProgressChanged事件,如何将此值传递给progressBar2?

One way is to use BackgroundWorker.ReportProgress(Int32, Object) and pass whatever you want, in the userState parameter. 一种方法是使用BackgroundWorker.ReportProgress(Int32,Object)并在userState参数中传递任何所需的内容。

Another is to simply know that when you've reached 100% on the first progress bar you need to increment the other bar. 另一个是简单地知道当你在第一个进度条上达到100%时,你需要增加另一个条。

ReportProgress has an overload which allows you to pass an extra object. ReportProgress有一个重载 ,允许您传递一个额外的对象。 You can use this to return any extra state you need. 您可以使用它来返回您需要的任何额外状态。

public void ReportProgress(
    int percentProgress,
    Object userState
)

You can then read the state from ProgressChangedEventArgs.UserState . 然后,您可以从ProgressChangedEventArgs.UserState读取状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM