Multithreading and Event handling
-
I have a problem, I need to run a series of little tasks. There are only about 4 types of task but they can be run against a number of databases. The exact number of instances of each task are unknown until run time. I could run all these tasks sequntially but this may take sometime and one or more may fail or timeout extending the runtime to collect all the data. So the obvious thing is to multithread, dynamically creating the threads and holding them in an array or collection. Most of this seems fairly easy, but the main problem I have is with handling any events e.g. successful termination. I can declare events for the class this is not a problem but to actually handle the event I have to specify a SUB to handle each specific instance .e.g. Sub TermEventHandler (ByVal intResult as Integer) Handles specific_instance.TermEvent This is of course impossible as the instance does not exist at compile time. What I need is a way of having just one SUB to handle all instances of a particular thread types TermEvent. I'm sure there must be a sensible way of doing this, but being a relative newbie to this I can't see it. I hope I have explained my problem in terms people can understand. If anyone could suggest a way out of this I would be grateful. Thanks
-
I have a problem, I need to run a series of little tasks. There are only about 4 types of task but they can be run against a number of databases. The exact number of instances of each task are unknown until run time. I could run all these tasks sequntially but this may take sometime and one or more may fail or timeout extending the runtime to collect all the data. So the obvious thing is to multithread, dynamically creating the threads and holding them in an array or collection. Most of this seems fairly easy, but the main problem I have is with handling any events e.g. successful termination. I can declare events for the class this is not a problem but to actually handle the event I have to specify a SUB to handle each specific instance .e.g. Sub TermEventHandler (ByVal intResult as Integer) Handles specific_instance.TermEvent This is of course impossible as the instance does not exist at compile time. What I need is a way of having just one SUB to handle all instances of a particular thread types TermEvent. I'm sure there must be a sensible way of doing this, but being a relative newbie to this I can't see it. I hope I have explained my problem in terms people can understand. If anyone could suggest a way out of this I would be grateful. Thanks
What i understood is that you want to create a different sub to handle each thread's events... if this is true.. I just ask why ? i think one handler is enough but you need to make some kind of synchronization search msdn for critical sections , mutex , etc.
-
What i understood is that you want to create a different sub to handle each thread's events... if this is true.. I just ask why ? i think one handler is enough but you need to make some kind of synchronization search msdn for critical sections , mutex , etc.
Thanks for the reply, maybe I wasnt too clear on my original statement. Its not that I want to handle each event seperately, it was that, I did not know how 1) make a sub handle more than one event and 2) how you do this when creating a thread from scratch. As it happens I found some thing on another site last night that showed me all I needed, I now understand a little more about dynamically creating objects and hooking things up to them etc. I did say that I was a newbie to all this:-D. I'll include what I found just in case anyone else is interested 1) you can say Sub TermEventHandler (ByVal intResult as Integer) Handles specific_instance.TermEvent anotherinstance.Termevent ... 2) To hook up the event dynamically AddHandler specific_instance.TermEvent, Addressof TermEventHandler