How to increase a progress bar?

the question might be seems to be a regular one, but it isn’t.

No matter what am I doing, the progress bar just not increasing.

In the base concept I have a GUI form and it has got a bacgroundworker. In the backgroundworker I am using a self defined class to read out the data from a measurement file. I want to increase the progress bar when it is done with one file, but no matter what am I doig it does nothing.

I have read the documentation but nothing.
https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-set-the-value-displayed-by-the-windows-forms-progressbar-control?view=netframeworkdesktop-4.8

I have tried:

worker.ReportProgress method

Controls.Invoke((MethodInvoker)delegate{}); method

Furhtermore tried the Control.Update() and Control.Refress() too, but these are not working too.

Please help!

Thank you all in advance!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The reason the Progress bar won’t update is that the UI cannot be updated from background threads. Backgroundworker runs on a background thread, which keeps the UI responsive.

Three solutions:

  1. In the backgroundworker, start a new Task that runs on the Main UI thread, and update the progress bar there. This is bad coding, but there are other use cases where .InvokeRequired is valid.
  2. Use backgroundworker ReportProgress. In the ProgressChanged handler (which runs on the UI thread), update the progressbar as normal.
  3. Rewrite your app to use the “await Function1()/ async Task Fuction1() pattern instead of Backgroundworker. Here .InvokeRequired will likely be used. You can also use EventHandlers to return responses to the caller.

Examples Follow

#1 Warning bad coding style!

private void bgw1_DoWork(Object sender, DoWorkEventArgs e)
{
    ...
    await Task.Run( () =>
    {
        progressBar1.InvokeIfRequired( o =>
        {
            progressBar1.Value += 1;
        } );
    });
    ...
}


public static class MyExtensions
{
    public static void InvokeIfRequired<T>(this T obj, InvokeIfRequiredDelegate<T> action) where T : ISynchronizeInvoke
    {
        if (obj.InvokeRequired)
        {
            obj.Invoke(action, new Object[] { obj });
        }
        else
        {
            action(obj);
        }
    }
}

#2 This way works well:

private void bgw1_DoWork(Object sender, DoWorkEventArgs e)
{
    ...
    worker.ReportProgress(0, 50);   //50% progress out of 100
    ...
}

private void bgw1_ProgressChanged(Object sender, ProgressChangedEventArgs e)
{
    if(e.UserState is Int32 i)
    {
        progressBar1.Value = i;
    }
}

#3

Is a complete re-write and likely outside the scope of your question.

Remember to mark this as an answer if it helps you.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x