The data cache is really the central controller of the whole thing, created
on the UI thread. It basically mediates between the UI components and the
worker thread and the worker thread does all the talking to the database.
The thread maintains queues of operations to perform. The data cache adds a
request to the queue when something needs to be done, for example:
User clicks ADD RECORD button on a toolbar
Code behind the toolbar click tells the cache to ADD RECORD
The cache formats a request and queues it at the thread,
theThread.AddRequest (theRequest, CRITICAL)
....then forgets about it.
I have 3 queues: CRITICAL, UI and BORED. Critical is for performing
operations the user requests, UI is for things like fetching thumbnails (in
my application this is a neccessary evil) and BORED is for coherency
checking and other stuff like that.
On each iteration of the worker thread loop ( while not m_bFinished () ), it
takes the single highest priority item from the list of queues and executes
it. If there is nothing in any of the lists, it idles for 100ms (so it
doesn't take up all CPU) - after idling it will choose something to check
coherency for (in my case the Tree Structure, Thumbnails, etc.) and creates
a request, adding it to the BORED queue. Then:
Thread de-queues the single highest priority request
Executes it
Invokes the result back to the cache
The cache resolves changes and fires events to it's listeners.
In terms of frequency, I leave this up to the user. You have to trade
coherency against network traffic and performance - my application is
designed to be able to connect to a webserver via. SOAP or ASP.NET, so it's
important I can throttle the coherency check down if I need to.
Note that even with high coherency, the user can still execute an action and
have it fail - ie. it's still possible to attempt to modify a record another
user has deleted, if you do it before the coherency check works out that the
record was deleted. To handle this case, you simply tell the user with an
error message, ie. "The record could not be modified because it no longer
exists".
It's really a simple pattern but implementation details can be more tricky.
If you need any help, post up
.