Partager via


Pool Manager Sample

File: ...\Samples\Solution\Ffc\_PoolManager.scx

This sample demonstrates how to use the Pool Manager class, which manages a pool or collection of objects from a class and is useful when you need to use objects repeatedly for a short amount of time.

For more information, see Pool Manager Foundation Class.

Pooling Objects

In this sample, forms are collected in a pool so that you can reuse any form that is available in the pool instead of having to create a new form when you select a customer from the drop-down list.

To use the Pool Manager class in this sample, the following Pool Manager class properties must be set in the Properties window:

cClass   = frmCustomer

cClassLibrary = frmCustomer.vcx

Binding to Pool Manager Events

You can bind to _PoolManager events. In this sample, the form's Init event contains code that binds the form's custom handler methods, PoolManager_ObjectRequested and PoolManager_ObjectReturned, to the _PoolManager ObjectRequested and ObjectReturned events so that status can be reported on the sample form:

BINDEVENT(This.PoolManager,"ObjectRequested",ThisForm,"PoolManager_ObjectRequested")
BINDEVENT(This.PoolManager,"ObjectReturned",ThisForm,"PoolManager_ObjectReturned")

Requesting Objects

To request an object from the pool, call the _PoolManager Get method.

To open a customer form in this sample

  1. Select a customer name from the drop-down list.
  2. Click View Customer.

You can open multiple forms by clicking View Customer repeatedly, which requests a form object from the Pool Manager class each time. The form's custom ViewCustomer method contains the following code:

loForm = ThisForm.PoolManager.Get(m.lcCustID)
If Vartype(m.loForm) == "O"           
   loForm.Show()
EndIf

In this sample, the Get method returns an unused object that exists in the pool or creates a new object. When a new customer form is created, code in the form's Init event binds to the _PoolManager ObjectRequested and ObjectReturned methods using the private variable THISPOOLMANAGER created in the Get method:

BINDEVENT(THISPOOLMANAGER,"ObjectRequested",This,"PoolManager_ObjectRequested")
BINDEVENT(THISPOOLMANAGER,"ObjectReturned",This,"PoolManager_ObjectReturned")

This event binding makes it possible for the form to call its Hide method instead of the Release method so the form returns to the pool instead of being destroyed.

Returning Objects

To return objects to the pool, call the _PoolManager Free method. In this sample, the form's QueryUnload event occurs when the customer form is closed and returns the form to the pool:

If Vartype(This.oPoolManager) == "O"
   This.oPoolManager.Free(This)
   NoDefault 
EndIf

See Also

Visual FoxPro Foundation Classes A-Z | Form Object Properties, Methods, and Events