Access to thread variables from invoked method

I will be using threads for a number of purposes, so I’ve made a variant of the TaskClass which, by using a delegate, I can have run an arbitrary method; I pass the addressof the method via the constructor and then invoke it in the thread. I want to add extra properties to the thread for use of the invoked method, but I seem unable to access them from within the invoked method. I tried (for example):

app.CurrentThread.someproperty = 0

but was told that the thread has no such property. Is there a way to achieve this?

Are you able to cast it?

If App.CurrentThread IsA MyTaskClass Then
  MyTaskClass(App.CurrentThread).SomeProperty = 0
End If

Paul,

Thanks yes that seems to work (i.e. the compiler is happy), but why?

Slightly more generally, I’d like to be able to have different properties available for different uses of the TaskClass. I’d settle for being able to have a generic object pointer as a property of TaskClass (a variant, perhaps?), which I could build on at runtime (as is possible in JavaScript). Hmmm, I suppose I could just have a plethora of classes containing different properties. Any comments?

The why is because App.CurrentThread is a Thread type. The Thread class does not have your custom property. But the subclass you created does. So you check to see if CurrentThread actually contains your subclass (using IsA) and if it does, you can cast the Thread down to the subclass.

This sounds messy. I’m not seeing the benefit over just putting the methods into their own subclasses with their own properties. You’ll get compiler type safety along with perhaps a better organizational structure.

OK - that makes sense :slight_smile:

One thing that’s driving this a bit, is this. In the existing PHP code that I’m porting, I have two modules that do almost the same thing, but one does it by reading the network, the other by reading from disk. They have similar but not quite identical outer parts, which call some functions that are identical (and which are by far the bulk of the code), and which at the bottom must either read from the network or from the disk. PHP has variable functions where the call is done using a variable (which must be set to a function name) rather than a fixed function name, but I can’t quite see a clean way to achieve this in the ported code.

Sounds like the right place to use an interface so whatever you pass in “does the right thing” but the code that uses it can blissfully ignore that one is a network and the other a disk

You might look at Readable & Writeable to get the idea

Norman: thanks for the pointer. That has simplified life a lot.