XLL+ Class Library

MtCalc sample

Functionality

The MtCalc sample contains an example of a one-hit asynchronous function. The add-in contains an extremely slow calculation for pricing an average equity option.

The add-in does all its calculation in background threads, so that the Excel interface is not tied up while the calculation runs. The user experience is as follows:

  1. While a calculation is running, the result cell displays "#WAIT!".
  2. When the calculation is complete, the result is updated.
  3. Thereafter, the cell is not updated any more, since the result is known, and will not change.

Note:- The calculator makes no claims to accuracy or efficiency. It's just a usefully slow calculation.

Running the sample

The sample files are provided in pre-built form in the Samples/Bin sub-directory. To run a sample, follow these steps:

  1. Open Samples/Bin/MtCalcGui.xla.
  2. Open Samples/Bin/MtCalcDemo.xls.
  3. Notice that the formulae cells initially show #VALUE!
  4. Observe that one by one, the formulae change to numbers.
  5. Try moving around the worksheet, doing standard Excel tasks like editing cells and entering formulae, and notice that the long slow calculations are not inhibiting the normal use of Excel.

Implementation

The add-in owns a number of background threads (currently four, but this can be easily changed).

The add-in puts new calculation requests into a queue. Whenever a background thread becomes available, the next calculation is dispatched to a new background thread by the StartCalculations() method.

When the calculation is complete, the results are posted back to the main thread and the thread dies.

When the main thread receives the results, they are put in the data cache.

The diagram below summarises the lifetime of a calculation data packet.

Content

The following source files are of interest:

MtCalc.h   Class definitions
MtCalc.cpp   C++ implementation of add-in
MtCalcGui.xla   User interface add-in

We will examine each in turn.

Next: MtCalc.h >>