BreakThread Issue

Hi

I have a TOracleQuery with
- Threaded:=True
- ThreadSynchronized:=True
- session.ThreadSafe:=True

The SQL in the query can change but it's always a select statement which I want users to be able to cancel the before it's returned all the results. To do this when the press the 'Stop' button I run the following code

Code:
LocalQuery.Session.BreakExecution;
LocalQuery.BreakThread;

For some reason if the query has finished running on the server and results have started to be brought back the BreakThread call doesn't seem to work. Stepping through the code once I get to the BreakThread line the application carries on and none of the lines under BreakThread get executed. This doesn
 
These break functions will only work when there is a database activity. When you are fetching records, there might not necessarily be a database activity. You will need to set an additional break flag, and the function that fetches the records will need to respond to this break flag so that it will stop.
 
Hi Marco

I already have a flag that I set when I call my CancelSearch Procedure - see below:

Code:
procedure TAdvancedSearch.CancelSearch;
begin
     FSearchStatus:=assCancel;
     if LocalQuery.ThreadIsRunning then begin
         OutputDebugString(PChar('ADVANCED_SEARCH: Cancel Search - Thread is running'));
         LocalQuery.Session.BreakExecution;
         OutputDebugString(PChar('ADVANCED_SEARCH: Cancel Search - DONE: Session.BreakExecution'));
         LocalQuery.BreakThread;
         OutputDebugString(PChar('ADVANCED_SEARCH: Cancel Search - Thread Broken'));
     end;
end;

And here is my code which handles the records as they are returned.

Code:
procedure TAdvancedSearch.ThreadRecord(Sender: TOracleQuery);
begin
     case FSearchStatus of
          assRunningMain: begin
                          Some Code ......
          end;
          assCancel: begin
                     Exit;
          end;
     end;
end;

From what I can tell though once I call BreakThread no more records are returned, and my third 'Thread Broken' debug line is never written out - see my output below. I can call the CancelSearch procedure repeatedly and I always get the same output but the thread says that it's still running.

Code:
Debug Output: ADVANCED_SEARCH: Cancel Search - Thread is running Process MyApp.exe (3248)
Debug Output: ADVANCED_SEARCH: Cancel Search - DONE: Session.BreakExecution Process MyApp.exe (3248)

Do you mean that I should call BreakThread from within the ThreadRecord Function something like the below?

Code:
procedure TAdvancedSearch.ThreadRecord(Sender: TOracleQuery);
begin
     case FSearchStatus of
          assRunningMain: begin
                          Some Code .......
          end;
          assCancel: begin
                     LocalQuery.BreakThread;
          end;
     end;
end;

Thanks for your help
 
Okay, I see now that you are using both LocalQuery.Session.BreakExecution and LocalQuery.BreakThread. You should only use LocalQuery.BreakThread for Threaded queries.
 
Hi Marco

Thanks for your reply. I've taken out the LocalQuery.Session.BreakExecution; but it's still not happy. After calling BreakThread neither the ThreadRecord,ThreadFinished or ThreadError events are called - and calling CancelSearch function again the LocalQuery.ThreadIsRunning still comes back as true. As I mentioned before this only seems to do this on some select statements after the records have started to be returned. I'm not sure why different Select SQL would make a difference to the BreakThread call?
 
If neither the ThreadRecord,ThreadFinished or ThreadError events are called, then the thread process would be hanging. Is that the case? Or does it continue?
 
It seems to hang. It doesn't even run the lines of code under the BreakThread call in my CancelSearch function. Is there any debug information I can get you that might help with this?
 
Does it help if you set ThreadSynchronized to False? It is easy to lock yourself when ThreadSynchronized = True.
 
Thanks Marco - that fixed the problem. The query stops straight away now. Will having ThreadSynchronized set to False not potentially cause me problems when the query interacts with the main thread?
 
You will either need to make all code in the event handlers thread safe when ThreadSynchronized = True, or you need to find out where things are deadlocked when ThreadSynchronized = False and fix that.
 
Back
Top