views:

631

answers:

3

I have written an application (using Delphi 2009) that allows a user to select a series of queries which can be run across a number of different systems. In order to allow queries to be run concurrently, each query is run in its own thread, using a TADOQuery object. This all works fine.

The problem that I have is when I try to close the application when a query is still running (and therefore a separate thread is active). When I create each thread, I record the thread's THandle in an array. When I try to close the application, if any threads are still running, I retrieve the thread's handle and pass it to TerminateThread, which should theoretically terminate the thread and allow the application to close. However, this doesn't happen. The main form's onClose event is triggered and it looks like the application is closing, but the process remains active and my Delphi interface appears as though the application is still running (i.e. "Run" button greyed out, debug view active etc.). I don't get control back to Delphi until I manually end the process (either Ctrl-F2 in Delphi or via Task Manager).

I am using TerminateThread because the query can take a long time to run (a few minutes in cases where we're dealing with a million or so records, which in the end user environment is perfectly possible) and while it is running, unless I'm mistaken, the thread won't be able to check the Terminated property and therefore won't be able to end itself if this were set to True until the query had returned, so I can't terminate the thread in the usual way (i.e. by checking the Terminated property). It may be the case that the user wants to exit the application while a large query is running, and in that case, I need the application to immediately end (i.e. all running threads immediately terminate) rather than forcing them to wait until all queries have finished running, so TerminateThread would be ideal but it isn't actually terminating the thread!

Can anyone help out here? Does anyone know why TerminateThread doesn't work properly? Can anyone suggest anything to get the threads running large ADO queries to immediately terminate?

Thanks for your help!

+4  A: 

As you can read in the msdn using TerminateThread is dangerous.

TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination.

But it also is very effective in killing threads. Are you sure you are right in your conclusions? Maybe the thread is killed, but another thread is still running? Maybe your handles are not thread handles? Could you show us some code? Or even better: A small working example we could try for ourselves?

Lars Truijens
I'm aware of the warning about using TerminateThread, but have explained my reasons for doing it above. The application closes fine when no threads have been kicked off to run queries, but doesn't close when I have a single thread running, so it is definitely the query thread that is causing the issue.I create the query thread and store the handle in an array like this:newThread := TQueryThread.create(true);threadStatusArr[index].threadHandle := newThread.handle;newthread.resume;I then retrieve the handle from the array and pass it to TerminateThread.
Jeedee
+7  A: 

You should be able to cancel an ADO Query by hooking the OnFetchProgress event, and setting the Eventstatus variable to esCancel. This should cause your query to terminate and allow the thread to close gracefully without having to resort to using TerminateThread.

Andy_D
This looks like the way forward. I've been working on something else for the past week or so but I'll look into this in the next few days. Have you got an example of any source for this?
Jeedee
+5  A: 

Instead of using threads with TADOQuery, maybe you should consider using the async options of ADO.

ADOQuery1.ExecuteOptions := [eoAsyncExecute, eoAsyncFetch, eoAsyncFetch];

Then when your application close, you can call :

ADOQuery1.cancel;
Pmax
I've already written the application using threads. It would take far too long to rewrite the querying to do it like this, unfortunately, and I'm not sure that it would work how I want it to anyway.
Jeedee