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:
Note:- The calculator makes no claims to accuracy or efficiency. It's just a usefully slow calculation.
The sample files are provided in pre-built form in the Samples/Bin sub-directory. To run a sample, follow these steps:
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.
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.