Right now i have a function that runs a query, using ADO, and returns a recordset:
Recordset Execute(Connection connection, String commandText)
{
//[pseudo-code]
Recordset rs = new Recordset();
rs.CursorLocation = adUseClient;
rs.CursorType = adOpenForwardOnly;
rs.Open(commandText, connection,
adOpenForwardOnly, //CursorType; the default
adLockReadOnly, //LockType
adCmdText);
return rs;
}
And this is fine. It runs synchronously, and returns the recordset of a query.
Now i want a similar version, that shows a ProgressDialog, providing the user with the ability to cancel a long-running query:
Recordset Execute(HWND parenthWnd, String caption, Connection connection, String commandText)
{
//[pseudo-code]
//Construct a progressDialog and show it
IProgressDialog pd = new ProgressDialog();
pd.SetTitle(caption); //e.g. "Annual Funding Report"
pd.SetCancelMsg("Please wait while the operation is cancelled");
pd.StartProgressDialog(parenthWnd, null, PROGDLG_MODAL | PROGDLG_NOTIME | PROGDLG_NOMINIMIZE, null);
pd.SetLine(1, "Querying server", False, null);
try
{
//Query the server
Recordset rs = new Recordset();
rs.Open(commandText, connection,
adOpenForwardOnly, //CursorType
adLockReadOnly, //LockType
adCmdText | adAsyncExecute);
while (rs.State and (adStateConnecting+adStateExecuting+adStateFetching) <> 0)
{
if pd.HasUserCancelled()
throw new EUserCancelledOperationException();
Sleep(100);
};
finally
{
//Hide and destroy the progress dialog
pd.StopProgressDialog();
pd = null;
}
//Now we have our results for the client
return rs;
}
The way to check if the user has cancelled the operation is to periodically ask the progress dialog if the user as pressed the cancel button:
pd.HasUserCancelled(); //returns true if user has clicked Cancel
Now i'm faced with how to periodically check if the user has cancelled (and to know if the query has completed, or an error has happened), and to be a good programmer and do it without polling.
The only way to know that an error has happened is to have a handler on the Recordset's FetchCompleteEvent:
pError
An Error object. It describes the error that occurred if the value of adStatus is adStatusErrorsOccurred; otherwise it is not set.
adStatus
An EventStatusEnum status value. When this event is called, this parameter is set to adStatusOK if the operation that caused the event was successfull, or to adStatusErrorsOccurred if the operation failed.Before this event returns, set this parameter to adStatusUnwantedEvent to prevent subsequent notifications.
pRecordset
A Recordset object. The object for which the records were retrieved.
So this would imply that i'm going to have to have my function construct a helper object, so i can have a FetchComplete
handler. But then i have to prevent my synchronous function from returning right away. And then we get into MsgWaitForSingleObject
, which is notoriously difficult to use correctly.
So i'm asking for assistance, or canned code.
i should be more explicit: i'm looking for an implementation of function with this method signature:
Recordset ExecuteWithCancelOption(Connection connection, String commandText)
that shows a dialog with a cancel button on it.
The challenge is that the function must now create whatever is required to achieve that. If that involves a hidden form, that has a timer on it, etc - okay.
But i'm looking for a synchronous function that displays a Cancel button.
And the function is going to be a near (or exact) drop-in replacement for
Recordset Execute(Connection connection, String commandText)
Given practical considerations on Windows, i would need to supply the function with a parent window handle that it will parent its dialog to:
Recordset ExecuteWithCancelOption(HWND parentHwnd, Connection connection, String commandText)
And given that this is going to be a reusable function, i'll let the caller provide the text that will be displayed:
Recordset ExecuteWithCancelOption(HWND parenthWnd, String caption, Connection connection, String commandText)
And given that these are both class functions in my TADOHelper
class, i can give them the same name, and have them be overloads of one another:
Recordset Execute(HWND parenthWnd, String caption, Connection connection, String commandText)
i would think in languages other than Delphi, anonymous delegates are helpful. But i'm still terrified of having to deal with MsgWaitForMultipleObjects
.
If it is Delphi you can drop a TTimer component in and use that to check if HasUserCancelled value is True. I don't have Delphi in front of me so I'd have to post an example later.
Edit:
Here's an example of a TTimer OnTimer event that checks the current time and the lastactivity time to decide what to do with the forms if the program has been left "Up":
In order to make GUI to respond to button clicks you should return control to the message loop of the window. While loop while (rs.State and (adStateConnecting+adStateExecuting+adStateFetching) <> 0) does not return control back to the message loop thus blocking GUI.
Below is an excerpt from a working Delphi code that uses asyncronous ADO queries. This code does not allow for non-modal fetching of data, but ensures that the main form is repainted during data fetch and also allows cancelling the query.
Asynchronous execution and fetching is achieved by setting:
FOpeningDataSet.ExecuteOptions := [eoAsyncExecute, eoAsyncFetchNonBlocking];
execution of query is cancelled by callingDataSet.Recordset.Cancel;
in
FetchProgress
event.Any TADODataSet shall be opened via the method:
Supporting code in the main form:
Progress form:
and dfm