0

I've a class with all my methods implemented , doing its job , etc. Then i have my Form1 - where i have a FileSystemWatcher component and some buttons\checkboxes instances.

I've been reading about BackgroundWorker , ProgressBar , but i can't figure out who should i implement in order to show the progress.

Since "everything happens" inside the .cs file (my class), is there an way to display its output ? And how is the FileSystemWatcher going to interact with it (everything happens after i call the watch method - before that nothing is done).

example: On my class, i've a function to Read Files , then another function for converting those files to XML. I would like to show the progress for each file being readed, then each file being converted. I got it done when i had a Console Application (found an method that did its job) but i can't understand how to implement it on a WinForm application.

If everything was inside the Form1.cs code sure it would be easier. I tried to make an "sample" because my original code is too big.

Code example:

public class Foo
{
//foo vars and methods...
}

And then, the form method

 public partial class Form1 : Form
    {
        private Foo prg;


        public Form1()
        {
            InitializeComponent();

            prg = new Foo();

            prg.LoadConfig();
            FillTextBox();

    }
       private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
//Do some checkbox verifications.... 
//after verification is done calls the Watcher (ProgramProcessing)
prg.ProgramProcessing(textBox1.Text);
}

What i want is to show a progress bar based on this watcher. Everything happens after he's triggered.

Edit: BackGroundWorker Sample Code with progressbar i've been trying to use:

   private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {



        // do some long-winded process here
        // this is executed in a separate thread
        int maxOps = 1000000;
        for (int i = 0; i < maxOps; i++)
        {
            rtbText.AppendText(i.ToString() + "\r\n");
            // report progress as a percentage complete
            bgWorker.ReportProgress(100 * i / maxOps);
        }
    }
    private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // update the progress bar
        pbProgress.Value = e.ProgressPercentage;
    }
    private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // return to "normal" mode of operation
        this.Cursor = Cursors.Default;
        //btnGo.Enabled = true;
    }
Pablo Costa
  • 125
  • 4
  • 14
  • Well as long as you can definitively predict in your process where you are, you could have a local of _progress which has the value, you could add a parameter of the progress bar, and then do thread safe calls to update the UI of with the progress of the progress bar. – BugFinder Mar 09 '16 at 15:19
  • The main form has no value inside it (only some textbox parameters, checkbox, etc). I may haven't understood your comment but all my progress happens outside the main form (a separated class). All examples i found are treating the entire code in one piece (one .cs file). And i don't know what is a thread safe call, kinda new to c# . My bad. – Pablo Costa Mar 09 '16 at 15:25
  • 1
    Ok, so where is your progress bar? Normally you'd have a progress bar on your main form, call "do really long stuff" on your class, either having given it a parameter or set a property to put the progress bar so it can easily access it. if you run it in a task etc the updates would then fall in a different thread to the UI and barf and whine you cant update it from there.. – BugFinder Mar 09 '16 at 15:27
  • ProgressBar is currently on the form (an component of course). I'll post a sample ProgressBar code i'm using. – Pablo Costa Mar 09 '16 at 15:33
  • 1
    You've mentioned running the thing in another thread - so you will need to read http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c - or have a progress property and a timer on the form with the progress bar – BugFinder Mar 09 '16 at 15:42
  • Just a clarification: My form1.cs is a thread , and my classes.cs is another thread ? So , instead of searching\reading about how to have a progressbar from another class, should i search on how to have a progressbar from another thread ? – Pablo Costa Mar 09 '16 at 15:47
  • 1
    Nope, by default classes are not separate threads. So if you arent making it in another thread my earlier comment would be irrelevant. But background workers are another thread. So.. yes, please read the link I posted earlier. – BugFinder Mar 09 '16 at 15:48

1 Answers1

1

If you want to use the latest .NET libraries, you can use IProgress interface and basically just call IProgress.Report(). This interface will save you the lines of code to implement the necessary event handlers using BackgroundWorker.

https://msdn.microsoft.com/en-us/library/hh138298(v=vs.110).aspx

Jay
  • 92
  • 3
  • I haven't checked it carefully yet but i think this will fit: http://stackoverflow.com/questions/19980112/how-to-do-progress-reporting-using-async-await - will try as soon i return from university – Pablo Costa Mar 09 '16 at 18:50