Example Raise Event from Class to a form

Hi could some one paste me a example on how to strucure and create an event which passes a message from class running on a seperate thread back to the main thread on a form which needs a progress box updating.

Hi,

An easy solution is to use a BackgroundWorker. (System.ComponentModel)
The BackgroundWorker allows you to execute code in a seperate thread.
It can report progress,and also when the code has finished executing via events.

It has three main events:

var BWorker:=New BackgroundWorker;

BWorker.DoWork+=MyWorker;
BWorker.RunWorkerCompleted+=MyWorkerDone;
BWorker.ProgressChanged+=MyWorkerProgressChanged;

BWorker.RunWorkerAsync; //Starts the Background worker

method MyWorker(Sender:Object;e:DoWorkEventArgs);
begin
//Anything executed here is run on the BackgroundWorker thread
// Progress changed events can be fired here to report progress back
end;

method MyWorkerProgressChanged(sender: Object; e:ProgressChangedEventArgs);
begin
//This runs on the main thread and so is safe to update the ui.
end;

method MyWorkerDone(sender:Object;e:RunWorkerCompletedEventArgs);
begin
//This runs on the main thread and so is safe to update the ui.
end;

I have attached three sample projects i knocked up when i was learning how to use it.
They are quick & dirty but should be easy to follow to get you started.

1= Simple wpf example.
2= As 1 but Win Forms.
3= Wpf example passing params in and out of Background worker.

Cheers
Paul

Thanks mate, thats great

No worries :slight_smile:

Sorry but where are the samples uploaded to ?
I’d love to try and learn this as well.