MainWindow Open Event Handler

Hello

I wrote a little application to switch on devices connected to a APC AP9212 PDU via SNMP calls.
As this device supports retrieving the current power status and description of the outlets, I placed the SNMP calls in the MainWindow Open Event Handler to draw the power on/off buttons state and the descriptions.

But as it turns out when starting the application for the first time, the first one or two SNMP calls are lost and it doesn’t draw all descriptions in the MainWindow.
Quitting and restarting the application is then just fine…seems caching helps to not loose any data.

Is there a better place to put those SNMP calls for retrieving UI description/button states?

Maybe use a Timer which starts shortly after the Window has been opened, instead of doing it in the open Event.

or

If you add a Thread to the Window and put the SNMP calls there, storing the received information in Properties of the Window and then when the SNMP calls have finished, the Thread starts a Timer with a Period of 0 in which the UI is then updated with the Values stored in the Properties? This would also prevent possible “freezing” while waiting for the results of your SNMP calls.

How about putting a flag in OPEN
then in ACTIVATE, if the flag is NOT set, then set the flag and do you tasks

this gives the app enough time to start up and initiailze itself

Tried a simple approach with a simple App.SleepCurrentThread(1000) before the SNMP calls…

The window isn’t visible until the 1 second has passed…and the first data is still lost…

Guess threading is the way then…

A timer is not threading…

A simple fact. :wink:

Yes I know…was just for a quick test if a longer timer delay would help…but it doesn’t (o;

For me this doesn’t sound as it would be related to the open event of a window.

How do you get the data? Declares into a dll/dylib? Via a shell? Via TCPSocket?

One second :wink:

App.Sleep.CurrentThread is NOT the same as a timer. In effect, it freezes execution for everything, while a timer does not. So in effect your sleep probably even made matters worse.

Try putting the SNMP call in a 20ms single timer event. That is plenty of time after the Action event.

Actually I use the pbswSNMP plugin…still works in Xojo 2014R3.2 though it is that old…

…with the MessageReceived event handler updating the MainWindow UI.

[quote=283212:@Michel Bujardet]One second :wink:

App.Sleep.CurrentThread is NOT the same as a timer. In effect, it freezes execution for everything, while a timer does not. So in effect your sleep probably even made matters worse.

Try putting the SNMP call in a 20ms single timer event. That is plenty of time after the Action event.[/quote]

That’s what i said before and putting the SNMP calls into a timer will also not work because this will also affect the Main Thread. Putting the SNMP stuff into a Thread and firing a Timer with 0 Period which will then update the UI, should work much smoother.

As i recommended in 2nd Post of this Forum Thread.

Please give it a try :slight_smile:

I will definitively give it a try as soon I figured out to to program threads/timers (o;

In the order of difficulty :

  1. Place the code in Activate
  2. Timer : drag a timer from the library onto the window, add an Action event, place your code there. Set it to Single with period 20 in the Inspector. That is 1 tick. Could probably be shortened. Action will take place 1/60th a second after the window opens.
  3. Thread. I personally tend to believe it is overkill.

Easy…

  1. Add Propertie(s) in which you can store the results from the SNMP calls, to your Window.
  2. Drag a Thread Object into your Window.
  3. Drag a Timer Object into your Window with Period 0 and Mode = Off
  4. Place your SNMP Code into the Run Event of this Thread. At the end of this Code call Timer.ModeSingle for your Timer.
  5. In the Timers Action Event, place your Code to Update the UI.

In the Windows Open Event, start the Thread with Thread.Run

(I assume this is a Single Window App. If it’s not, better store your Values in a Property of a Global Module.)

You are not allowed the Access UI Elements from within a Thread. This is why we use Properties of a Window for the SNMP Results and a Timer to update the UI. You are allowed to Start, Reset, … a Timer from within a Thread, because it’s not a UI Element. :wink:

If you want to prevent the User from using UI Elements before the SNMP stuff has finished, start with these Elements in Disabled State and set them to Enabled in the Timer. Maybe you should also add a Note somewhere in the Window, telling the User that the App is still starting up and remove this Note in the Timer.