Reputation: 13296
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?
Upvotes: 2
Views: 1129
Reputation: 17427
don't forget that to use ReportProgress()
method you must set WorkerReportsProgress
as true
.
Upvotes: 2
Reputation: 13296
Here's a tutorial of how to use backgroundworker with ProgressBar
You should call (RunWorkerAsync) first then the backgroundPBar_DoWork will start executing its code.
backgroundPBar.RunWorkerAsync();
Upvotes: 0
Reputation: 1286
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
Upvotes: 0
Reputation: 81610
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").
Upvotes: 2