Problem closing thread application inside a component

Asked

Viewed 637 times

0

I am developing a component (visual indicator) that to search the data in the database and generate the indicator itself uses a Thread. This Thread is inside the component itself. It works fine, except when I close the application and Thread is running. There occurs the error "Runtime 216".

Details:

  • In the creation of Thread it is set as FreeOnTerminate;
  • If there is only one component (visual indicator) in the form, can close the application smoothly. The problem occurs when there is more than one component;

Follows excerpt from the code:

constructor TWThread.Create(CreateSuspended: Boolean);  
begin  
  inherited Create(CreateSuspended);  
  FreeOnTerminate := True;  
  oQryThread      := TTIQuery.Create(nil);  
  oCdsThread      := TClientDataSet.Create(nil);  
  oDBThread       := TTIDatabase.Create(nil);  
end;

procedure TTIWidget.EventoOnTerminate(Sender: TObject);  
begin  
  if Assigned(oThread.oQryThread) then  
  begin  
    if oThread.oQryThread.Active then  
      oThread.oQryThread.Close;  
    FreeAndNil(oThread.oQryThread);  
    if Assigned(oThread.oCdsThread) then  
      FreeAndNil(oThread.oCdsThread);  
    if Assigned(oThread.oDBThread) then  
      FreeAndNil(oThread.oDBThread);  
  end;  
  oThread := nil;  
end;  
  • You could not terminate these threads in the Ondestroy event of the Form where the components are?

  • Yes, but I want to do something more generic so I don’t mess with the application where the components are. Thanks for the help.

  • You can descend all the Foms of your application from a generic form that checks whether these components are specifically running and attempts to terminate them.

  • You could post the Create/Execute/Terminate snippet from your thread ?

  • Edited question

  • How do you define the FreeOnTerminate, what happens if you take the oThread := nil; ?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.