Does URLConnection.Send("Get…) always run in the main thread? I have an app that calls it in a separate thread at startup but the app seems to be unresponsive until the thread/call finishes. The particular (and only) thread runs at main window open and does nothing except make the URLConnection.Send call. So, if the URLConnection call isn’t running in the main thread, I must have something else happening.
I can’t say for sure but I have noticed that I can’t make any UI updates in the same method that I use to perform URLConnection.Send().
For example, if I attempt to make a progressWheel visible and disable the data fields in the window and then Send, it won’t work. I must create two methods… one to change the UI and call the second which performs the send.
Here is how I’ve been working with URLConnection: (I’d love to hear any feedback regarding best practices so let me know if you have a better way)
Consider a basic data workflow in which you have two windows, ListWindow and EditWindow.
In the ListWindow, I have a ListBox called, “List”… a URLConnection called, “Connection”… and a ProgressWheel called, “Wheel”.
In the Activate event of the ListWindow, I clear the ListBox and make the ProgressWheel visible before calling the ListRecords method that does the work of sending the request to my REST server.
Sub Activate() Handles Activate
List.DeleteAllRows
Wheel.Visible = True
ListRecords
End Sub
Rather than include the send command in the activate handler, I created a method within the ListWindow that Activate can call…
Public Sub ListRecords()
Connection.Send("GET", url)
End Sub
I then use the ContentReceived event in the URLConnection to check the HTTPStatus before processing the response and hiding the ProgressWheel.
Sub ContentReceived(URL As String, HTTPStatus As Integer, content As String) Handles ContentReceived
Dim data As New JSONItem(content)
Select Case HTTPStatus
Case 200
Receive(data)
Wheel.Visible = False
Else
Close
Caution(str(HTTPStatus), data.Lookup("error_message", content))
End Select
End Sub
I’m not sure if this answers your question but it’s how I’ve managed to work with URLConnection in such a way that it doesn’t lock up my UI. Hope this helps.
Interesting approach, Kristin. In my case, I used a bit of subterfuge and created a Splash screen for the app and put the URLConnection code in that window. The splash screen starts a timer and fires the URLConnection code. The timer is set to a length of time suitable for the user to view splash window. As soon as both the timer expires and the URLConnection code finishes, the splash screen closes itself and opens the main application window.