Question:
When an asynchronous Chilkat method is called, callbacks will be in the background thread. The background thread is generally unknown to the application runtime. In VB6 one must be extremely careful in what is done within the event callback. For example, UI controls, such as text boxes, etc. should not be accessed from within a background thread. The UI controls should only be accessed from the UI (main) thread.
The same applies for DoEvents. Do not call DoEvents from a background thread.
Important: Never use asynchronous methods when there is no purpose. Specifically, if your application is calling the asynchronous method, and then simply waiting for it to complete (by spinning with a Sleep delay), then the better choice is to call the synchronous method with AbortCheck callbacks. This way all of your VB6 code runs in the main UI thread, including the AbortCheck callbacks.
This is bad:
Private WithEvents http As ChilkatHttpPrivate Sub http_AbortCheck(abort As Long) ' This callback is running in a background (worker) thread when called ' from an asynchronous method that is itself running in a background thread. ' (the callback happens in the same thread as the caller) abort = 0 End Sub
Private Sub http_TaskCompleted(ByVal task As Chilkat_v9_5_0.IChilkatTask) ' This callback is running in a background (worker) thread when called ' from an asynchronous method. ' (the callback always happens in the same thread as the caller) End Sub ...
Public Function Something() Dim request as new ChilkatHttpRequest ... Dim task As ChilkatTask Set task = http.PostUrlEncodedAsync("https://somedomain.com/xyz/abc", request) If task.Run = 1 Then Do While task.Finished <> 1 task.SleepMs 100 DoEvents Loop End If
...
End Function
There is no point in running your code in a background thread if your code is simply spinning in the foreground thread to wait for completion. This is the equivalent of running synchronously -- but just more complicated and open to all sorts of other problems. Don't do it.
Simplify by calling the method synchronously, and use periodic AbortCheck callbacks (which are in the same thread as the caller -- i.e. the foreground main UI thread).
This is better:
Private WithEvents http As ChilkatHttpPrivate Sub http_AbortCheck(abort As Long) ' This callback is running in the foreground thread (main UI thread) because it is called ' from a synchronous method call that is running in the main thread. abort = 0
' Call DoEvents here to keep the UI responsive... DoEvents
End Sub...
Public Function Something() Dim request as new ChilkatHttpRequest ...
' Get AbortCheck callbacks every .1 seconds. http.HeartbeatMs = 100
Dim response as ChilkatHttpResponse Set response = http.PostUrlEncoded("https://somedomain.com/xyz/abc", request)
... End Function