简体   繁体   中英

C# BackgroundWorker ReportProgress Int32?

I want to display progress from a BackgroundWorker to a ProgressBar in the UI. What I dont understand is why the BackgroundWorker 's ReportProgress function takes an Int32 when the ProgressBar can take a double value for progress. My process is very intricate and I need to make decimal progress updates. It doesnt help if I have to keep adjusting everything to the nearest integer. Can anyone suggest someway i can work around this?

The ProgressChangedEventArgs Class can take an optional UserState value. You can pass anything you want to this (including for example a double value). Set it with the ReportProgress method

ReportProgress also has a second variant where it takes an object as an additional parameter:

public void ReportProgress(
    int percentProgress,
    Object userState
)

Simply pass your double as the userState object.

On a side note: Are you sure your Progress bar has so many pixels that you need a double to enumerate them all?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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