The implementation for Thing
handles we have
discussed so far is not necessarily efficient.
Look at the screen-shot below.
There are four identical calls to Thing.Create()
,
and they create four separate objects. This is wasteful of
memory and can be bad for performance if the handle creation function is expensive.
However, we can clear up this problem instantly by using a results cache. Set the Cache results feature for the object creation function:
Now only one Thing handle will be created for any unique set of inputs:
Note: Do not attempt to save and restore the results cache. All it will contain are handles, which are, in effect, simply pointers to memory addresses that are no longer valid. Make sure that you do not check the "Save the cache to file" check-box in the XLL+ AppWizard.
(If you have already created the project, just remove any calls to CXlOperCache::Save() and CXlOperCache::Restore().)
You should switch on the feature "Do not call in Formula Wizard", otherwise many unnecessary handles will be created.
Add-in functions that create handles may not be marked as ThreadSafe. This is a restriction of Excel that applies to the RTD mechanism.
However, functions that merely use handles as arguments can be marked as thread-safe.
If the handle creator function is slow, you can add an asynchronous function which is executed in a background thread. See Asynchronous functions for more information. See the MonteCarlo sample for an example of a handle creation function that has an asynchronous version.