I’m trying to build a thread in a Module programmatically (syntax below), similar to creating a Timer and handling it’s .Action event. Can a thread be created on the fly and can you code it’s Run behavior in an AddHandler?
// Scanner is a property of a Module with a method called ScannerAction
Scanner = new Thread
AddHandler Scanner.Run, AddressOf ScannerAction
Looks fine to me. As per Timer, you need to remember to add the Sender As Thread parameter to your ScannerAction method.
Yep, you’re right - my code did work… except to test it I broke the golden rule and had it updating a UI element! My bad! I’m writing for RPi platform and unable to directly test compile, so I tried writing a test method and mistakenly had it piping out updates to a ListBox for rough testing - which of course failed since you aren’t supposed to update the UI via Threads. Once I got my coffee I used an intermediate variable and all worked as it should.
And if ScannerAction belongs to a class, use WeakAddressOf instead of AddressOf.
I appreciate the tip Kem! I’ll make that change right now…