login about faq

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 ChilkatHttp

Private 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 ChilkatHttp

Private 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

asked Jul 20 at 12:42

chilkat's gravatar image

chilkat ♦♦
11.8k316358420

edited Jul 20 at 13:16

Be the first one to answer this question!
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×35
×19
×14
×12
×7

Asked: Jul 20 at 12:42

Seen: 1,287 times

Last updated: Jul 20 at 13:16

powered by OSQA