[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Progress bar on Form / Progress in Class Function

Posted on 2012-08-17
5
Medium Priority
?
817 Views
Last Modified: 2012-08-18
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.
0
Comment
Question by:Evan Cutler
  • 3
5 Comments
 
LVL 14

Expert Comment

by:Vel Eous
ID: 38306811
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
 
LVL 9

Author Comment

by:Evan Cutler
ID: 38306966
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 38307175
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
 
LVL 9

Author Comment

by:Evan Cutler
ID: 38308073
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
 
LVL 9

Author Closing Comment

by:Evan Cutler
ID: 38308127
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Progress

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question