The code fragment below shows how the GenericService methods can be implemented in a C++ component. This example uses a static Boolean instance variable, _stop, to indicate when the service should cease running. The start() method sets the _stop variable to false; start() must also perform any other necessary initialization that are needed by your service, such as opening files, database connections, and so forth. run() executes a while loop as long as the _stop variable is false. In each loop iteration, run() performs some of the work that the service is responsible for, such as refreshing a copy of a remote database table, then calls the JagSleep C routine to relinquish the CPU. The stop() method sets the _stop variable to true. stop() must also clean up any resources that were allocated in the start() method.
#include <jagpublic.c> // For JagSleep API
class MyService
{
private:
static boolean _stop; // Declared static in case multiple
// instances are run.
public:
void start()
{
_stop = false;
... perform necessary initializations ...
}
void stop()
{
_stop = true;
}
void run()
{
while (! _stop)
{
... do whatever this service does
on each iteration ...
JagSleep(1000);
}
... perform necessary cleanup and deallocations ...
}
};
| Copyright © 2003. Sybase Inc. All rights reserved. |
|
|