Archived Forum Post

Index of archived forum posts

Question:

Event Callbacks for Asynchronous Methods?

Dec 22 '14 at 12:45

I am using the Chilkat Socket asynchronous methods. To implement a progress Bar I tried using the event callback functions (in C++).

    socket.put_EventCallbackObject(&myProgressObj);
    socket.put_HeartbeatMs(200);
where myProgressObj is an instance of a class derived from CkBaseProgress. I am calling the AsyncReceiveBytesN method to read an amount of binary data, but no callback methods are being called. What's wrong?


Answer

There are no event callbacks for asynchronous methods. The definition of an event callback has to do with a single thread of execution. The thread of execution proceeds like this:

  1. Your app calls a (synchronous) Chilkat method. The thread of execution is now within the internals of the Chilkat method.
  2. An event callback is fired. This is a call back into your application's code (to the event callback handler method). The thread of execution is now within your app's code.
  3. Your app's callback handler returns. The thread of execution has returned back to the internals of the Chilkat method call.
  4. The Chilkat method call returns. The thread of execution has returned back to your app's code.

When an asynchronous method is called, Chilkat creates a background thread to run the task. The method that initiates the async task returns immediately to your application (in this case, AsyncReceiveBytesN returns and a separate background thread is doing the ReceiveBytesN). There cannot be a callback from the background thread to your app's foreground thread, because they are separate threads of execution. (Technically it's possible, but all sorts of problems can arise depending on the application's architecture, programming language, etc. where app code in a background thread is restricted especially when it comes to accessing UI elements.) The foreground thread of your application can choose to periodically check the current status of a background (async) task by examining whatever properties might be available for such a thing: such as by examining the socket.ReceivedCount property.