Thread in own class


if i create a thread within my own classes, how can I set his run methods within code? If i create a timer, I set his action event via AdressOf. How to do that for a thread?


Thanks Marius,

followed the instructions.

Created a new class Importer with a property mSplitThread As Thread and a SplitLines Method. Class constructor looks like this:

mSplitThread = New Thread AddHandler mSplitThread.Run, AddressOf SplitLines

Compiler says

Type mismatch error. Expected delegate Delegate( Thread ), but got delegate Delegate( ) AddHandler mSplitThread.Run, AddressOf SplitLines

What’s to do?

You SplitLines method has to have a parameter of type Thread. You use that where you would usually use Me or Self in the subclass.

Ok Tim thanks. Now it works after add parameter SplitThread(sender As Thread)

Why is a parameter required here, but not in this case:

Is it because it’s in a constructor, or was there a change in Xojo?


The calling instance is always part of AddHandler. William did not write out his scannerAction method verbosely. It must have been (at least) ScannerAction (Sender as Thread). The same is valid for every AddHandler, so for a Button.Action event the button becomes the sender etc.
See the LR about AddHandler:

[quote]The delegateMethod method declaration must consist of:
A reference to the object being handled. This is the sender object and it must be the first parameter in the delegate and must be the correct type. You can get this reference using AddressOf or WeakAddressOf.
… [/quote]

Thank you :slight_smile: