I have multiple instances of a myThread that are spawned off by myClass.
MyClass defines a method that I pass to addhandler to handle the event generated by the myThread.
As the event handler receives the instance of the myThread that raised it, multiple threads can use the same method. Correct?
But must myClass keep the instances of myThreads it creates?
for i=0 to n
dim t as new myThread
AddHandler t.EventName, AddressOf EventHandler
MyClass.MyProc didn’t keep the refrences to the threads it created. Is that bad?
I can imaging if MyClass goes out of scope then that might lead to issues if those threads decide to raise their events.
What I’ve done is each time I start a new thread I add the thread to an array of threads that is a member of MyClass.
At some point when a thread finishes I would like to remove that thread from the array of active threads. It would seem that the best place to do this would be the last line of the thread’s run method, but it seems odd to me to do it this way…
(Likewise if MyClass decides to destruct then it can kill all its active threads, before exiting.)
Thank you for the comment on the WeakAddressOf but I’m not sure I get why this is ‘better’.
Hmm… Ok. None of the examples I’ve looked at for AddHandler seem to use WeakAddressOf (Like for dynamically created controls that have events, timers, container controls etc…) It would seem reasonable to assume that each instance of MyThread has the address of the method of the instance of MyClass’s event handler.
But not all thread’s are guaranteed to belong to the same instance of MyClass and therefore the address of MyClass::MyHandler could be different as MyHandler is not shared or static.
Should I just do a blanket search and replace of all AddressOf to WeakAddressOf?
How should I check to see if all my threads are completed?
Do I use a timer and loop through the array of thread references and check the state of each? I’m still confused about removing the reference to the thread from the array of thread instances.
No, choosing AddressOf or WeakAddressOf is a decision that has to be made on a case by case basis. Threads have a nice clean point of termination where you can call RemoveHandler and avoid circular references – if your code is structured such that you know the handler to remove. In other situations, you may not have this luxury and really do need to use a weak reference.
As for knowing when all the threads are completed, you could have a method that you call at the end of each thread’s Run event (or the handler you’ve installed) that loops through your array of threads and checks their state. The one thing I want to point out is that you need to be careful about making sure the loop doesn’t cause yielding, probably by using a CriticalSection around the loop.
I’d love for there to be a detailed section in the documentation about reference reference counting. Someone should file a Feedback ticket if they also feel that way.
[quote]NSString* s = [[NSString alloc] init]; // Ref count is 1
[s retain]; // Ref count is 2 - silly
// to do this after init
[s release]; // Ref count is back to 1
[s release]; // Ref count is 0, object is freed[/quote]
The Language Reference says: “It can only be used with instance methods of classes” and for me makes sense to use WeakAddressOf instead of AddressOf when the method is an instance of a class.
One thing is not full clear to me about this:
If I use, say, a Thread as a property of a class, then I wire the Run event with AddHandler and WeakAddressOf, do I have still to RemoveHandler before the class is destroyed (and the Thread instance too)?