Mohgeroth
Mohgeroth

Reputation: 1627

C# Progress Bar within a class?

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.

Upvotes: 2

Views: 1057

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273244

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.

Upvotes: 2

spinon
spinon

Reputation: 10847

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.

Upvotes: 3

Related Questions