I create a background thread to do some work and, at the very end, I call a method ThreadDone(threadWorkResult)
which raises an event. Currently, the event handler runs on the same background thread but I would like it to run on the main UI thread (Forms application). I searched online and found something about using attributes here but would like to know how to do this programatically. Is there any way it can be done outside the body of the handler itself?
I looked into BackgroundWorker but I have to create several threads at once so all of the respective OnWorkerCompleted event handlers become quite messy; more importantly, not all of them require the completed event. Worst case scenario I will use several BackgroundWorkers but is it possible for me to simply call a method (void aMethod()
) from a background thread and force it to run on the primary UI thread?