✅ BackgroundWorker getting progress

I have a winform applications and with this I have put a ProgressBar on it. I was manually changing the progress inside functions using progressBar.Value = <value> but now that I run my functions in the background using BackgroundWorker, changing that progress is a little harder than I thought. If I am not mistaken the code below is defined as a 'Delegate Invocation' and I use it to run the function I would like to track progress of:
c#
public void ModifyTQWorker(object sender, DoWorkEventArgs e)
{
try
{
object[] args = e.Argument as object[];
if (args != null && args.Length == 4)
{
ClientContext context = args[0] as ClientContext;
Microsoft.SharePoint.Client.List listObj = args[1] as Microsoft.SharePoint.Client.List;
string TQNumber = args[2] as string;
User Raisedby = args[3] as User;

if (context != null && listObj != null && TQNumber != null && Raisedby != null)
{
(sender as BackgroundWorker).ReportProgress(10);
ModifyTQ(context, listObj, TQNumber, Raisedby);
}
else
{
MessageBox.Show("One of the parameters was not given for the ModifyTQWorker.", "Error");
}
}
}catch(Exception ex)
{
MessageBox.Show($"There was an issue modifying the current TQ.\n{ex.Message}", "Error");
}
}
c#
public void ModifyTQWorker(object sender, DoWorkEventArgs e)
{
try
{
object[] args = e.Argument as object[];
if (args != null && args.Length == 4)
{
ClientContext context = args[0] as ClientContext;
Microsoft.SharePoint.Client.List listObj = args[1] as Microsoft.SharePoint.Client.List;
string TQNumber = args[2] as string;
User Raisedby = args[3] as User;

if (context != null && listObj != null && TQNumber != null && Raisedby != null)
{
(sender as BackgroundWorker).ReportProgress(10);
ModifyTQ(context, listObj, TQNumber, Raisedby);
}
else
{
MessageBox.Show("One of the parameters was not given for the ModifyTQWorker.", "Error");
}
}
}catch(Exception ex)
{
MessageBox.Show($"There was an issue modifying the current TQ.\n{ex.Message}", "Error");
}
}
How can I give progress better?
6 Replies
leowest
leowest9mo ago
Why harder? The worker thread u pass the current progress value i.e.:
worker.ReportProgress(value);
worker.ReportProgress(value);
Then on the report event u update the ui
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
there is a full sample here https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?view=net-8.0
SparkyCracked
SparkyCrackedOP9mo ago
Thank you will try this And read this...I keep ignoring the documentation. My bad
jcotton42
jcotton429mo ago
you should not be using BackgroundWorker @SparkyCracked you should instead use Task.Run, CancellationToken, and IProgress
jcotton42
jcotton429mo ago
Task.Run vs BackgroundWorker: Intro
This is an introductory post for a new series that I’ll be doing comparing BackgroundWorker to Task.Run (in an async style). I always recommend Task.Run, and I have already written a long post describing why, but I still see some developers resisting the New Way of Doing Things (TM). So this will be a short series where I’ll compare the code sid...
jcotton42
jcotton429mo ago
they're much nicer to use, and are applicable outside of GUIs
SparkyCracked
SparkyCrackedOP9mo ago
Ok I see I think thats why I am having issues with my events as well. It's really killing me. Task.Run was so much better The main reason I use background worker in this case is just to understand it a little more as the company I am at used it in the code they wrote, and I wanted to see if it was useful. I think imma try improve the existing code they have to Task.Run instead of BGWorker Thanks for the help @jcotton42 and @leowest, let the learning continue :copium:
Want results from more Discord servers?
Add your server