Call a user-supplied function when DB-Library is reading from the server.
void dbsetbusy(dbproc, busyfunc) DBPROCESS *dbproc; int *(*busyfunc)())();
A pointer to the DBPROCESS structure that provides the connection for a particular front-end/server process. It contains all the information that DB-Library uses to manage communications and data between the front end and server.
The user-supplied function that DB-Library will call whenever it accesses the server. DB-Library calls busyfunc() with a single parameter—a pointer to the DBPROCESS from the dbsetbusy call.
busyfunc() returns a pointer to a function that returns an integer.
None.
This routine associates a user-supplied function with the specified dbproc. The user-supplied function will be automatically called whenever DB-Library is reading or waiting to read output from the server. For example, an application may want to print a message whenever the server is accessed. dbsetbusy will cause the user-supplied function busyfunc() to be called in this case.
Similarly, dbsetidle may also be used to associate a user-supplied function, idlefunc(), with a dbproc. idlefunc() will be automatically called whenever DB-Library has finished reading output from the server.
The server sends result data to the application in packets of 512 bytes. (The final packet in a set of results may be less than 512 bytes.) DB-Library calls busyfunc() at the beginning of each packet and idlefunc() at the end of each packet. If the output from the server spans multiple packets, busyfunc() and idlefunc() will be called multiple times.
Here is an example of defining and installing busyfunc() and idlefunc():
The application functions busyfunc() and idlefunc() are callback event handlers and must be declared as CS_PUBLIC for the Windows platform. For portability, callback handlers on other platforms should be declared CS_PUBLIC as well.
/*
** busyfunc returns a pointer to a function that
** returns an integer.
*/
int (*busyfunc())();
void idlefunc();
int counterfunc();
...
main()
{
DBPROCESS *dbproc;
...
dbproc = dbopen(login, NULL);
/*
** Now that we have a DBPROCESS, install the
** busy-function and the idle-function.
*/
dbsetbusy(dbproc, busyfunc);
dbsetidle(dbproc, idlefunc);
dbcmd(dbproc, "select * from sysdatabases"); dbcmd(dbproc, " select * from sysobjects");
dbsqlexec(dbproc);
/*
** DB-Library calls busyfunc() for the first time
** during dbsqlexec(). Depending on the size of the
** results, it may call busyfunc() again during
** processing of the results.
*/
while (dbresults(dbproc) != NO_MORE_RESULTS)
dbprrow(dbproc);
/*
** DB-Library calls idlefunc() each time a packet
** of results has been received. Depending on the
** size of the results, it may call idlefunc()
** multiple times during processing of the results.
*/
...
}
int CS_PUBLIC (*busyfunc(dbproc))()
DBPROCESS dbproc;
{
printf("Waiting for data...\n");
return(counterfunc);
}
void CS_PUBLIC idlefunc(procptr, dbproc)
/*
** idlefunc’s first parameter is a pointer to a
** routine that returns an integer. This is the same
** pointer that busyfunc returns.
*/
int (*procptr)();
DBPROCESS *dbproc;
{
int count;
printf("Data is ready.\n");
count = (*procptr)();
printf ("Counterfunc has been called %d %s.\n",
count, (count == 1 ? "time" : "times"));
}
int counterfunc()
{
static int counter = 0;
return(++counter);
}