XLL+ Class Library (6.3)

MonteCarlo2 Sample

Combines the use of object handles, and asynchronous functions, and also provides a persistent object cache

Overview

This add-in demonstrates the combined use of:

Requirements

To edit the functions in this add-in, you need to load the extension file RtdHandles.xpe. See Loading an extension file for instructions.

You should also make sure that neither of the following extension files is loaded, since the various types of handles are mutually exclusive.

In addition, at run-time the RTD Async Function Server DLL, XllRtdLink.dll, should be registered. On a machine where the XLL+ toolkit has been installed, this DLL should already be registered. See Requirements for asynchronous functions for details.

At run-time the RTD Handle Server DLL, RtdHandleServer.dll, should be registered. On a machine where the XLL+ toolkit has been installed, this DLL should already be registered. See Object handles: Requirements and deployment for details.

Persistent objects

This project is identical to the MonteCarlo sample, with one difference: each MCResults object is saved to an object cache. As a result, when a spreadsheet is reopened, the MonteCarlo formula is not recalculated.

For a full discussion of persistent object handles, refer to the topic Persistent object handles in the XLL+ User Guide.

The code below is the entire implementation of the persistent object cache:

CopyC++
// Serialization functions for the MCResults class 
inline CXlIStream& operator>>(CXlIStream& is, MCResults*& t)
{
    std::vector<double> values;
    is >> values;
    t = new MCResults(values);
    return is;
}
inline CXlOStream& operator<<(CXlOStream& os, const MCResults* t)
{
    os << t->AllValues();
    return os;
}

// A serialized object cache for PersistentThing instances 
const TCHAR* pszCacheFile = _T("%TMP%\\MCResults_MonteCarlo2.dat");

psl::PersistentHandleCache<MCResults> thePersistentCache(
    NULL,         // Use the default handle formatter
    pszCacheFile, // Data file 
    100,          // Maximum number of items in saved file 
    0,            // Maximum age in seconds (ignored if 0) 
    true);        // AutoSave

Clearing the cache

When clearing the cache, the features must be cleared in the following order:

  1. RTD Link - first stop all running worker threads
  2. Results cache (if used)
  3. Handle cache

In this sample, the code is as follows:

CopyC++
::XllGetTypedApp()->m_rtd.Clear();
::XllGetTypedApp()->m_cache.ClearFunction(L"MonteCarlo");
psl::ClearHandleCache<MCResults>();

Classes and functions used

PersistentHandleCache<T> | CXlIStream | CXlOStream

Sample project

Each sample project is located in a sub-directory of the Samples directory of the XLL+ installation. To use the sample project, open the solution file MonteCarlo2.sln or the project file MonteCarlo2.vcproj.

You can enable debugging under Excel by using the Setup Debugging command in the XLL+ ToolWindow.

When delivered, the help files are excluded from the build. You can enable the help build by selecting the files MonteCarlo2.help.xml and MonteCarlo2.chm in the Solution Explorer, and using the right-click menu to view Properties. Select the page "Configuration Properties/General" and set the "Excluded from build" property to "No". See Generating help in the User Guide for more information.

See Also

List of Sample Projects | MonteCarlo sample | Persistent object handles | Asynchronous functions