Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backgroundworker, not running the progressbar

How can I fix this issue ?

I am expecting the progressbar to load during process untill process it is done

Here is my code:

private void btnProcess_Click(object sender, EventArgs e)
    {
        backgroundWorker.WorkerReportsProgress = true;
        backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
        backgroundWorker.DoWork += backgroundWorker_DoWork;

        backgroundWorker.RunWorkerAsync();
    }

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        //start transaction
        DoTransaction();
    }

    private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar.Value = e.ProgressPercentage;
    }

My transaction function:

private void DoTransaction()
    {
        string pathIdentifier;
        pathIdentifier = func.checkthePathFile();
        if (pathIdentifier == null)
        {
            MessageBox.Show("Path has no yet been specified!");
        }
        else
        {
            //create xml base from user inputs
            XElement transactXML = new XElement("Transaction",
                new XElement("CardNumber", txtCardNum.Text.Trim()),
                new XElement("ExpireDate", txtExpDate.Text.Trim()),
                new XElement("Cardtype", txtCardType.Text.Trim())
                );

            //save xml to a file
            transactXML.Save(pathIdentifier + "/sample.xml");
        }
    }
like image 522
GrayFullBuster Avatar asked Aug 15 '26 19:08

GrayFullBuster


2 Answers

How is the runtime supposed to know how far along your process is?

You need to tell it by calling backgroundWorker.ReportProgress from the background operation. No magic here.

MSDN: http://msdn.microsoft.com/en-us/library/ka89zff4.aspx

Break down your process into meaningful chunks and ReportProgress whenever it makes sense to do so.

public void DoTransaction()
{
    part1();
    backgroundWorker.ReportProgress(25);

    part2();
    backgroundWorker.ReportProgress(50);

    part3();
    backgroundWorker.ReportProgress(75);

    part4();
    backgroundWorker.ReportProgress(100);
}

Edit Based on Posting of Transaction() function

If you are not confident in writing multithreaded programs, then do not attempt to write multithreaded programs, even with the help of a BackgroundWorker which tries to abstract some of those details away from you.

A few issues:

Your provided Transaction() method attempts to launch a MessageBox and read the Text property of various controls from the background thread. This is going to cause problems as the runtime typically throws an Exception when UI elements are accessed from a thread other than the one which created them.

If you really want to do the XML saving in the BackgroundWorker, you should validate the filename and directory, and save the Text properties to an intermediate object before setting up the BackgroundWorker and calling RunWorkerAsync.

Furthermore, in my opinion, your Transaction method is not going to be time intensive enough to truly warrant a background thread. Even a relatively old PC will be able to create and save a 15 element XML file faster than you can blink. The runtime will probably waste more time marshalling data between the threads than it would to simply write the file out to disk. Just do your work in the button click event handler.

like image 199
akatakritos Avatar answered Aug 17 '26 08:08

akatakritos


needs some reference to the BackgroundWorker instance.pass the reference to the class when instantiating it.
instantiate like this

BackgroundWorker worker = sender as BackgroundWorker;


then call like this

`worker.ReportProgress(...)`
like image 42
Ravindra Bagale Avatar answered Aug 17 '26 09:08

Ravindra Bagale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!