Page 1 of 1

Basic Example of Threaded SDAC with Grid and BreakExec

Posted: Mon 25 May 2020 16:17
by coder6910
Please provide an example of SDAC using a threaded query that populates a grid and can be cancelled with a breakexec. The threaded example in demos has a stop button - but does not use a breakexec pattern. Also, your help document doesn't have enough detail on how to do this.

Regards,
Pat

Re: Basic Example of Threaded SDAC with Grid and BreakExec

Posted: Mon 01 Jun 2020 08:52
by Stellar
You can run the query in a separate thread and, if necessary, terminate execution of the query in the main thread of the application.
For example:

Code: Select all

type
  TExecThread = class(TThread)
  private
    FQuery: TMSQuery;
  public
    constructor Create(const Query: TMSQuery);
    destructor Destroy(); override;

    procedure Execute; override;
  end;

implementation

{$R *.dfm}

{ TExecThread }

constructor TExecThread.Create(const Query: TMSQuery);
begin
  inherited Create(True);

  FQuery := Query;
end;

destructor TExecThread.Destroy;
begin
  FQuery := nil;

  inherited;
end;

procedure TExecThread.Execute;
begin
  inherited;

  FQuery.Open;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  Thread: TExecThread;
begin
  MSConnection1.Connect;
  MSQuery1.SQL.Text := 'SELECT * FROM Table_Name';

  Thread := TExecThread.Create(MSQuery1);
  Thread.FreeOnTerminate := True;
  Thread.Start;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  MSQuery1.BreakExec;
end;