Quick addHandler / thread question...

Just to make sure I have this right before I do it… if I have multiple threads in an array, this is acceptable:

addHandler workerThread(threadIndex).run, addressOf self.handleWorker

Each instance of the ‘handleWorker’ thread will run in it’s own thread, correct? Or is this a no go, as if one thread already initiates the addHandler then the address of handleWorker is already occupied with its work?

One other… if ‘handleWoker’ accepts parameters, such as:

referenceObject as thread, workType as byte, targetContent as memoryBlock

How would I construct the addHandler to accept those? Sorry, not familiar enough with addHandler… and trying to overcome it. Anything I try, I usually get this error:

Type mismatch error. Expected Delegate(Thread), but got Delegate(Thread, uInt8, memoryBlock)

I think you need to add an event definition to your thread class with the extra parameters. Then in the run event you can raise the new event with the needed parameters and use addHandler to handle the new event.

You can also do this with a delegate. I just did this yesterday ironically.

  1. In the thread class, define a delegate that matches the signature of the method you will be calling. If you don’t know, you could pass a jsonitem or dictionary.
  2. Add one property to the thread for each parameter in your delegate.
  3. Create two Constructors. Once which is private which has no parameters (so it can’t be called), and one which is public with the same list of parameters as the delegate plus a parameter of the delegate type, in which you set each of the parameters. You’ll also need a property to hold the value of the passed delegate.
  4. Implement the Run event so that it calls the delegate, using the properties for each of the parameters.

The nice part of this is that you just pass in the address of the callback method when you call the constructor.

dim th as new myThread(Params, addressOf myCallback) Th.run

Perfect, thank you Greg and Jim!

BTW, Greg… worked like a charm!