简体   繁体   中英

backgroundWorker_ProgressChanged not working?

I am having a trouble with background worker

private void backgroundPBar_ProgressChanged(object sender, ProgressChangedEventArgs e)

The function doesn't execute when backgroundPBar.ReportProgress(value);

here's the code:

private void backgroundPBar_DoWork(object sender, DoWorkEventArgs e) {
    while (fileTransfer.busy) {
        if (fileTransfer.sum > 0) {
            int value = Convert.ToInt32((fileTransfer.sum * 100) / fileTransfer.fileSize);
            backgroundPBar.ReportProgress(value);
            Console.WriteLine(value);
        }
    }
}

private void backgroundPBar_ProgressChanged(object sender, ProgressChangedEventArgs e) {
    progressBarFile.Value = e.ProgressPercentage;
    this.Text = e.ProgressPercentage.ToString() + "%";
}

How can I fix it?

Make sure you have the progress event wired up:

backgroundPBar.ProgressChanged += backgroundPBar_ProgressChanged

From you description, it seems like it isn't. The assumption here is that fileTransfer.busy is true and fileTransfer.sum > 0 is also true.

Also, make sure you have your the properties of the background worker set:

backgroundPBar.WorkerReportsProgress = true;

(as I see "The Mask" mentioned").

不要忘记,要使用ReportProgress()方法,必须将WorkerReportsProgress设置为true

I've been having problems getting the ProgressChanged event to fire immediately when the ReportProgress method is called. Because they are seperate threads, I had to resort to thread synchronization. I used a couple WaitHandles and the SignalAndWait method to synchronize the two threads. I kinda defeats the purpose of the background worker, but if I really want the UI thread to do something on time, it really has to make the background worker wait until the UI thread got the message and performed the work.

    Private ReportProgressWaitEvent As New AutoResetEvent(False)
    Private ReportProgressReadyWaitEvent As New ManualResetEvent(False)
    Private Sub ReportProgress(backgroundWorkerProgressEvents As BackgroundWorkerProgressEventsEnum, userState As Object)
        ReportProgressReadyWaitEvent.Reset()
        _backgroundWorker.ReportProgress(backgroundWorkerProgressEvents, userState)
        WaitHandle.SignalAndWait(ReportProgressReadyWaitEvent, ReportProgressWaitEvent)
    End Sub
    Private Sub _backgroundWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles _backgroundWorker.ProgressChanged
        ... do work ...
        ReportProgressReadyWaitEvent.WaitOne()
        ReportProgressWaitEvent.Set()
    End Sub

Here's a tutorial of how to use backgroundworker with ProgressBar

You should call first then the backgroundPBar_DoWork will start executing its code. 然后backgroundPBar_DoWork将开始执行其代码。

backgroundPBar.RunWorkerAsync();

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