Progress bar on Form / Progress in Class Function

Greetings.
I have a progress bar on Form1.cs

A button on Form1.cs calls the "click" event, which creates an object

DAT2XLS  document = DAT2XLS(sourcefile, schemafile);

Then it executes the function:

DAT2XLS.doyourwork();

I am attempting to do this in the background.

I need to have doyourwork() report progress.
Since it is in a different class, I don't know how to tell the progressbar what the progress is.

I appreciate any wisdom on this.

Thank you.
LVL 9
Evan CutlerVolunteer Chief Information OfficerAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
In your class, define a delegate and your custom event:
    public class DAT2XLS
    {

        public delegate void Percentage(int value);
        public event Percentage Progress;

    }

Open in new window


Then, in your processing loop, you can do:
                int percentComplete = 50; // <-- calculate the percent complete somehow
                if (Progress != null)
                {
                    Progress(percentComplete); // <-- raise your custom event with the current percent complete
                }

Open in new window


Back in your main form, you wire up the Progress() event and then echo it with your BackgroundWorker():
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DAT2XLS D2X = new DAT2XLS(TXTBinFile.Text, TXTXMLSchemaFile.Text);
            D2X.Progress += new DAT2XLS.Percentage(D2X_Progress); // <-- wire up the custom Progress() event
            D2X.ETLRUN();
        }

        private void D2X_Progress(int value)
        {
            backgroundWorker1.ReportProgress(value); // <-- echo the progress to the BackgroundWorker()
        }


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

Open in new window

0
 
Vel EousResearch & Development ManagerCommented:
You could create a custom event handler in your class doing the work, then your form can reference it and become aware when the event is triggered.

http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-custom-event-handlers
0
 
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
ok.
I read the article, but a bit confused.
Here's my form code:

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DAT2XLS D2X = new DAT2XLS(TXTBinFile.Text, TXTXMLSchemaFile.Text);
            D2X.ETLRUN();
        }


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

Open in new window



and Here's my class (some of it):

    public class DAT2XLS
    {
        String DatFile;
        String SchemaFile;

        public DAT2XLS(String Dat, String Schema)
        {
            DatFile = Dat;
            SchemaFile = Schema;
        }

        public void ETLRUN()
        {
                //sudocode here
                open file;
                totallines;  //gives the "100%"
                start file back at top;
                linecount = 0;
                do while (!end of file)
                         read line, do something //gives current line
                percent = linecount / totallines;
                SEND PERCENT VALUE BACK TO PROGRESSCHANGED
        }

Open in new window


ok...how do I tell ETLRUN() to throw the percentage back to ProgressChanged in the form class?  Thanks.
0
 
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
Thanks for all of your help...
I'm having issues still

D2X_Progress is not sending the value to ProgressChanged (I think)...
or ProgressChanged is not accepting something.

The PBXFR.Value is not updating.

Thank you so much for walking me through this.
0
 
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
Never mind, it was my calculations that didn't push the value.
THANK YOU SO MUCH!!!!

This worked perfectly.
This is an invaluable tool.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.