How to pas a TCP object name to a TimerAction method

Hello,

I’am new with Xojo and I wonder how I, in a console application, I can use a TCP socket inside a TimerAction method when the TCP object was declared inside the App Run Event.

Th emain code App RunEvent is :

MyTimer = New Timer

MyTimer.Period = (25)
MyTimer.Mode = 2
MyTimer.RunMode = Timer.RunModes.Multiple
AddHandler MyTimer.Action, AddressOf TimerAction

z.NetworkInterface = System.NetworkInterface(0)
z.Address = “localhost”
z.Port = 7000
z.Listen

And the following code inside TimerAction doesn’t work; I get an error “Cannot get this property’s value” for z

if (z.IsConnected) then
z.Write(“test”)
end if

Is someone can help me to solve this issue ?

Thank you infinitely,

Jean

Simplest way is to make both be properties of the application. Then you refer to it thus:

app.MyTimer = New Timer
app.MyTimer.Period = 25

...

app.z.NetworkInterface = System.NetworkInterface (0)

etc, and similarly in the TimerAction method.

ALSO: when posting code, please do this:

  1. enter your code in the post
  2. Select your code with the mouse
  3. Click the </> button

Thank you for your answer and advices, unfortunately, the problem is the same and now,
I get errors also in the Run Event method.

I joint a screen capture to help seeing the problem.

Thank you

So, after some try and with your advices, it work now !!! :blush:

In order to do with ‘app.’ , the following declaration worked:

z = New PC

but, this one, not:

Var z as new PC

Previously, without the Timer and all the related stuff (so , just the TCP socket), the code worked with:

Var z as new PC

At this time, I don’t understand why.

When the Run event handler of the app completes, any variable declared within it will die. So if you want those variables to exist for the Timer event handler, they must be made external to that method - the simplest way is to make them properties of the application. In that case, don’t declare them in the Run event handler.

A property declared for the app must still be initialised, so you still need:

z = new TCPSocket

otherwise it is Nil.

If you use Var z then you are not using the Property you added in App here:
image

you are creating a new one to be used in the Run event only.

Even methods called from within the Run event won’t be able to access variables declared within the event.
For this, you can either pass the variable as a parameter, or declare the parameter as a property of the surrounding class instead.

Thank infinitely for all of your valuable answers, I understand better now !

These are very interesting explanations and I learned a lot from all of you.

Like always, coming from another IDE or language need some time and learning curve to be proficient :wink:

1 Like

Can you also show your App Properties?

Thanks

Hello,

Here is a screen capture of the working code.

Thank you

Jean