How to add display caching


Summary
This article shows how to add display caching to your application.

Adding display caching

Some drawing sequences can take awhile to complete. A simple way to optimize your application is to enable display caching. This refers to the ScreenDisplay's ability to record your drawing sequence into a bitmap and use the bitmap to refresh the picture box's window whenever the Paint method is called.
The cache is used until your data changes and you call IScreenDisplay::Invalidate to indicate that the cache is invalid. There are two kinds of caches:
The following code uses a recording to implement a display cache in the application's Paint method:
[Java]
static void picture1Paint()throws Exception{
    if ((m_pScreenDisplay.isCacheDirty((short)esriScreenCache.esriScreenRecording))){
        m_pScreenDisplay.startRecording();
        // Make a call to your Draw function. For example:
        myDraw(m_pScreenDisplay, picture1.getHDC());
        m_pScreenDisplay.stopRecording();
    }
    else{
        tagRECT rect = new tagRECT();
        m_pScreenDisplay.drawCache(picture1.getHDC(), (short)
            esriScreenCache.esriScreenRecording, rect, rect);
    }
}
When you execute this code, you will see that nothing is drawn on the screen. This is due to the ScreenRecording cache not having its dirty flag set. To ensure that your Draw function is called when the first paint message is received, you must invalidate the cache. For example, add the following line to the end of a Form_Load method:
[Java]
m_pScreenDisplay.invalidate(null, true, (short)esriScreenCache.esriScreenRecording);
Utilizing multiple caches
Some applications (for example, ArcMap) can require multiple display caches. To utilize multiple caches, follow these steps:
  1. Add a new cache using IScreenDisplay.AddCache. Save the cache ID that is returned.
  2. To draw to your cache, specify the cache ID to StartDrawing.
  3. To invalidate your cache, specify the cache ID to Invalidate.
  4. To draw from your cache, specify the cache ID to DrawCache.
To make your application support its own cache, add a member variable to hold the new cache. See the following:
[Java]
private short m_lCacheID;
Create the cache in a Form_Load method. See the following:
[Java]
m_lCacheID = m_pScreenDisplay.addCache();
Change the appropriate calls to use the m_lCacheID variable and remove the start and stop recording from the previously used Paintmethod.


See Also:

How to redraw the display




Development licensingDeployment licensing
ArcGIS for Desktop BasicArcGIS for Desktop Basic
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine