[COLOR="Green"]// Event args to hold data for event
[/COLOR]class ProgressEventArgs : Event Args {
public float Progress {get; set; }
}
[COLOR="Green"]// This class raises the event that will cause your form to be updated
[/COLOR]class BackgroundOperation {
[COLOR="Green"] // This event will be handled by the form
[/COLOR] public event EventHandler<ProgressEventArgs> ProgressChanged;
[COLOR="Green"] // other stuff[/COLOR]
private void UpdateProgress(float percent){
[COLOR="Green"] // Cache event delegate to avoid subtle threading issues[/COLOR]
var eventDelegate = ProgressChanged;
if(eventDelegate != null) { [COLOR="Green"]// If there are any registered handlers (i.e. your form)...[/COLOR]
var args = new ProgressEventArgs(); [COLOR="Green"]// Create event data[/COLOR]
args.Progress = percent;
eventDelegate(this, args);[COLOR="Green"] // Raise event[/COLOR]
}
}
}
[COLOR="Green"]// Your form class handles the ProgressChanged event and updates the progress bar[/COLOR]
class FormOrOtherUIClass : Whatever {
[COLOR="Green"]// Add the event handler where you create the worker[/COLOR]
public void CreateWorker() {
[COLOR="Green"]// ...create worker...[/COLOR]
worker.ProgressChanged += Worker_ProgressChanged;
[COLOR="Green"]// remember this event handler will need to be unregistered when[/COLOR]
[COLOR="Green"]// you're done with the worker.[/COLOR]
}
[COLOR="Green"] // Handles the ProgressChanged event of the worker [/COLOR]
void Worker_ProgressChanged(object Sendor, ProgressEventArgs e){
UpdateProgressBar(e.Progress);
}
[COLOR="Green"] // This function updates the progress bar. If we call it from the wrong thread,
// it just re-routes it to the correct thread.
[/COLOR] void UpdateProgressBar(float percent) {
if(InvokeRequired) [COLOR="Green"]// (if wrong thread)[/COLOR]
[COLOR="Green"]// Call this same function on the main thread.[/COLOR]
this.Invoke(new Action<float>(UpdateProgressBar), percent);
else {[COLOR="Green"] // (if correct thread)[/COLOR]
[COLOR="Green"] // Caluclate progress bar value[/COLOR]
int newValue = (int)(percent / 100 * ProgressBar1.Maximum);
[COLOR="Green"]// Clamp value to be safe[/COLOR]
newValue = Math.Min(newValue, ProgressBar1.Maximum);
[COLOR="Green"]// Update progress bar[/COLOR]
progressBar1.Value = newValue;
}
}
}