How can I use Chilkat as Async to get data from website via VB6?

163 views Asked by At

I want to read the data of a text file on the server and display it in my program using Active X Chilkat. I used the Http.QuickGetObjAsync method to read the text and I expected that with this method the program will not be locked until reading the text. But the program is locked until the text is read and displayed.

This is my code :

Dim WithEvents Http As ChilkatHttp
Dim Http As New ChilkatHttp

Dim task As ChilkatTask
Set task = Http.QuickGetObjAsync("http://www.example.com/notification.txt")   
If (Http.LastMethodSuccess <> 1) Then
Debug.Print Http.LastErrorText
Exit Sub
End If


success = task.Run()
If (success <> 1) Then
Debug.Print task.LastErrorText

Exit Sub
End If

Do While task.Finished <> 1
'  Sleep 100 ms.
task.SleepMs 50
Loop

If (task.StatusInt <> 7) Then
Debug.Print "Task did not complete."
Debug.Print "task status: " & task.status

Exit Sub
End If

Dim resp As New ChilkatHttpResponse
success = resp.LoadTaskResult(task)

 MsgBox resp.Body 

 If (success <> 1) Then
 Debug.Print resp.LastErrorText
 Exit Sub
 End If

 Debug.Print "status code: " & resp.StatusCode
 Debug.Print "response header: " & resp.Header
 success = glob.FinalizeThreadPool()

I also used this code. But it seems that this event is not implemented and has no effect.

Private Sub http_TaskCompleted(ByVal task As Chilkat_v9_5_0.IChilkatTask)
Dim response As New ChilkatHttpResponse
Dim success As Long
success = response.LoadTaskResult(task)

If (success <> 1) Then
    Debug.Print response.LastErrorText
    MsgBox response.Body 
    Exit Sub
End If
Debug.Print response.Body 
End Sub

Is there a solution to prevent the program from locking up while reading the text?

Note : Chilkat Version :9.5.0.91

1

There are 1 answers

2
Chilkat Software On

The TaskCompleted callback is called from a background thread, which doesn't play nicely with VB6. You wouldn't want to use it.

A better way is to use the AbortCheck event (callback) with a HeartbeatMs property value. See https://chilkatsoft.com/chilkat_vb6_keep_ui_from_locking_up.asp