100 Days of PowerBuilder – Tutorial – Day 7 – Opening a Window

Posted on Tuesday, March 30th, 2021 at 5:13 pm in

This is part of my project ‘100 Days of PowerBuilder’ which is a series of discussions focused on basic PowerBuilder development.

Note: This article is written with examples created in PowerBuilder 2019 R3. Most steps/examples will be identical with any version going back to 9.

Although we have already opened the main window for our application earlier, there are some things to consider when opening windows. One of the chief concerns is the ‘responsiveness’ of the window. By this I mean what does the user see when the window opens. Does it sit there with a ‘busy’ type icon spinning away and then open? Let’s conduct an experiment.

Open the w_main form for our application and place a Multi-Line Edit control on it.

Expand it so you can see a fair amount of text and make sure the Horizontal Scrollbar property is checked. Now some code in the Open event of the window.

long ll_i, ll_et
string ls
time lt_1, lt_2< httpclient lnv_http
lnv_http = CREATE httpclient lt_1 = Now() // start time
// connect to the web service and send the request
ll_i = lnv_http.sendrequest('Get',"http://worldtimeapi.org/api/timezone/america/new_york")
IF ll_i <> 1 THEN
messagebox('http','request failed')
RETURN
END IF
// get the JSON response from the web service
lnv_http.getresponsebody(ls)
// end time
lt_2 = Now() ll_et = SecondsAfter(lt_1, lt_2)
mle_1.text = 'Start: ' + string(lt_1) + '~r~nEnd: ' + string(lt_2) + &
'~r~nElapsed: ' + String(ll_et) + '~r~n~r~nResponse: ' + ls IF IsValid(lnv_http) THEN DESTROY lnv_http

So what this code does is connect to a web service to get the current time in New York City and then puts the response JSON text into the MLE control.  Prior to connecting to the web service it gets the time and also gets the time after the call so we can see how long the ’round trip’ took.  This information is also put into the MLE text property.

Now when you run this you will get various response times based on your connection speed and latency of the web service (amongst other various things).  Regardless of the speed the window did not open until the code in the open event concluded which makes the application appear unresponsive.

To ‘improve’ the responsiveness of the application we are going to create a separate event on the window; this is known as a ‘user event’.  Click on the dropdown arrow next to the event name ‘Open’, scroll up and choose ‘(new event)’.

Now give it the name ‘ue_postopen’ (notice the top dropdown is updated when you tab off the name field.  In keeping with PB conventions the event name is prefaced with “ue_” to indicate it is a user event – you could also name it “we_” since it is a window event.  In any case none of the other parameters/etc. are changed.

Now cut the code from the ‘Open’ event and paste it here.

In the now empty ‘Open’ event place this line:

EVENT POST ue_postopen()

Or alternatively:

THIS.PostEvent("ue_postopen")

Now when you ‘post’ an event or function call it is placed onto the call stack and then executed once the current method is completed (or after other events and functions if they are already on the call stack since it is a ‘first in, first out’ type of arrangement).

When you run the application now you will see the window opens immediately and then some time passes before the MLE text is updated.  In my testing it took anywhere from 2 to 30 seconds (the default timeout).  Depending upon the situation you may have additional information messages show in the application screen so that the user understands that something is going on behind the scenes.

This type of post open functionality is normally part of most window ancestors and it used whenever any potentially long process is performed as part of the initialization of the object.  These include database calls (populating datawindows/datastores), retrieving network resources (files, images, etc.) or really anything which may take more than a second or so.

Add your comment

Leave a Reply

Top