views:

69

answers:

2

Simply put, what is a good way to send the progress of some process back to a form from within a class outside the scope of the form?

EG: I have a Input object, a filepath is sent into the constructor of this object and it parses the file. I want to show the progress of reading in the lines of this file back to the user who is in a form outside the scope of the currently running method. Ultimately I will be displaying overall progress and per file progress.

+3  A: 

Add an event to your class that the form can subscribe to. UpdateProgress or something like that. Your class would then raise the event every so often to let the form display the progress.

spinon
To add to this answer: this won't work unless your processing is in a separate thread, and the UI thread continues to run (in order to update the progress bar). You must also Invoke() when updating the progress bar as the event handler will be called in the processing thread, not the UI thread. See also: BackgroundWorker.
Jason Williams
@Jason thanks. I didn't think to mention that.
spinon
strictly speaking you could call Application.DoEvents() in your event handler. But I wouldn't recommend that, the OP needs a thread.
Henk Holterman
+2  A: 

The best approach here would be a BackgroundWorker. It has a specal event ReportProgress (that handles the Invoke logic for you).

It is an easy way to do multi-threading. Follow a good example when implementing the RunWorkerCompleted event.

Henk Holterman
Very simple to implement this, good stuff!
Mohgeroth