Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a long running method , so created a progress bar to show what percentage my method was to completing but I am having difficulty figuring out how to since my progress bar with my method => excelHelper.InsertNewRows();.

What I have tried:

Code:

public void ButtonSubmit_Click(object sender, EventArgs e)
    {
        if (isProcessRunning)
        {
            MessageBox.Show("A Process is aleady running");
            return;
        }


        Thread backgroundThread = new Thread(
            new ThreadStart(() =>
            {
                for (int n = 0; n < 100; n++)
                {

                    isProcessRunning = true;
                    Thread.Sleep(50);
                    progressBar1.Invoke(
                        new Action(() =>
                        {
                            progressBar1.Value = n;
                            label3.Text = ("Progress: " + n+ "%");

                        }

                     ));
                }
                MessageBox.Show("Thread completed!");
                progressBar1.Invoke(
                    new Action(() =>
                    {
                        progressBar1.Value = 0;

                    }
                    ));
                isProcessRunning = false;

            }
            ));
        backgroundThread.Start();
        excelHelper.InsertNewRows();
        var folder = TextOutputFile.Text + @"\" +
        DateTime.Now.ToString("yyyy_MM_dd_") + "SA_Analysis_Report.xlsx";
        excelHelper.Save(folder);
        MessageBox.Show("File has been added to file");
    }
Posted
Updated 8-Aug-19 6:17am

If you want the progress bar to match the progress of your method, then you'll need to update your method to report its progress. Use the IProgress<T> interface and the Progress<T> class to manage the communication.

IProgress<T> Interface (System) | Microsoft Docs[^]
Progress<T> Class (System) | Microsoft Docs[^]

If you want your UI to update whilst the method is running, you'll need to run it on a background thread. Eg:
C#
public void ButtonSubmit_Click(object sender, EventArgs e)
{
    SubmitAsync();
}

private async Task SubmitAsync()
{
    if (isProcessRunning)
    {
        MessageBox.Show("A Process is aleady running");
        return;
    }
    
    var progress = new Progress<int>(n =>
    {
        progressBar1.Value = n;
        label3.Text = ("Progress: " + n+ "%");
    });
    
    string outputFile = TextOutputFile.Text;
    
    isProcessRunning = true;
    
    await Task.Run(() =>
    {
        excelHelper.InsertNewRows(progress);
        
        string fileName = Path.Combine(outputFile, DateTime.Now.ToString("yyyy_MM_dd_") + "SA_Analysis_Report.xlsx");
        excelHelper.Save(fileName);
    });
    
    MessageBox.Show("File has been added to file");
    progressBar1.Value = 0;
    isProcessRunning = false;
}
NB: The callback passed to the Progress<T> class is automatically invoked through the SynchronizationContext captured when the instance is constructed. You don't need to worry about using Invoke to update the UI in the callback.
 
Share this answer
 
I am not sure what your actual problem is, but there are many examples of how to use ProgressBars to be found at c - Google Search[^]
 
Share this answer
 
Comments
KGr28 6-Aug-19 10:24am    
My question is:

I have a long running method, so I created a progress bar to show what percentage my method was to completing, but I am having difficulty figuring out how to do this ,syncing my progress bar with my method => excelHelper.InsertNewRows();.
Basically my progress bar representing how long the function has to run.
Richard MacCutchan 6-Aug-19 10:47am    
Follow the link above; the first item even has the sample code you need.
KGr28 6-Aug-19 10:52am    
The issue isn't getting the progressbar to work it is getting it to run synchronous with my function so:

What I want it to do:

When I click on the submit button I want it to fire both my ProgressBar and method at the same time , the progressBar should Monitor the length of time it takes my method to complete from 0% to 100% .

What it is doing:
When I click on the ButtonSubmit_Click button it hits my thread with my code for my progressBar ignores it, then it statrts my method , without loading my progressbar


The Link you provided doesn't show how to do that.
Richard MacCutchan 6-Aug-19 11:11am    
Here is a CodeProject article with full source code: BackgroundWorker and ProgressBar demo[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900