XLL+ Class Library

RTD mechanism

The add-in uses a very simple implementation of Excel’s real-time data (RTD) interface. RTD is a pull-push protocol: Excel periodically enquires of a server whether there is any new data available with regard to a particular topic; the server responds with a list of the data it would like to push into Excel.

Communication between worker thread and RTD

The mechanism used to pass messages from a worker thread and RTD must be thread-safe. The implementation chosen is to use a shared file containing a sequence number. When the worker thread needs to indicate that something has happened, that Excel needs to know about, it does the following:

  1. Locks the appropriate file.
  2. Reads the number contained in it.
  3. Increments the number by one and writes it to the file.
  4. Unlocks the file.

The RTD module is periodically interrogated by Excel, to ask whether there is any new information of interest. The module responds by reading the appropriate shared file and checking the sequence number contained therein. If the number is higher than it was the last time the module looked at it, it informs Excel.

All the code for the communication mechanism is contained in the header file Include\XllRtdLink_common.h.

Granularity

RTD is capable of supporting complex hierarchies of topics. However, the implementation in this sample does not make use of this complexity. The only message it passes to Excel is:

"A function with the name ‘AvgOpt’ has completed."

Alternatively, we could have chosen to pass the message:

"A function ‘AvgOpt’, called by a particular cell, has completed."

We have chosen not to specify which AvgOpt function has completed, and as a result, if there are multiple instances of the function in a spreadsheet, Excel will refresh all of them. While this is not optimal if there are many cells depending on the results of these calculations, it was considered worthwhile because:

  1. The use model in the spreadsheet is extremely simple. The asynchronous functions can be called in a way that is only very slightly different from calling their synchronous equivalents.
  2. The code for the implementation in the add-in function is much simpler.
  3. The performance cost of repeating the calculations for cells which have already completed is tiny: the add-in function just looks up the results in the cache.

Next: AvgOptData class >>