Einzelnen Beitrag anzeigen

Edelfix

Registriert seit: 6. Feb 2015
Ort: Stadtoldendorf
220 Beiträge
 
Delphi 10.4 Sydney
 
#11

AW: FireDac Asynchron Abort

  Alt 15. Jun 2023, 16:13
Es muss ein SQL Request sein der länger dauert. Wenn man dann wie wild auf Button1 klickt dann kommt eine Exception.

Eine Datenbank einbinden und sonst ein Beispiel:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.VCLUI.Wait,
  FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, Data.DB,
  FireDAC.Comp.DataSet, FireDAC.Comp.Client, Vcl.StdCtrls, Vcl.Grids,
  Vcl.DBGrids, FireDAC.Phys.ADS, FireDAC.Phys.ADSDef;

type
  TForm1 = class(TForm)
    Button1: TButton;
    FDConnection1: TFDConnection;
    FDQuery1: TFDQuery;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    procedure Button1Click(Sender: TObject);
  private
    procedure FDQuery1AfterOpen(DataSet: TDataSet);
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FDQuery1AfterOpen(DataSet: TDataSet);
begin
  DataSource1.DataSet := FDQuery1;
  FDQuery1.ResourceOptions.CmdExecMode := amBlocking;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if (FDQuery1.Command.State <> csInactive) then
  begin
    FDQuery1.Connection.AbortJob(true);
    FDQuery1.Connection.Close;
  end;
  DataSource1.DataSet := nil;
  if FDQuery1.Active then
    FDQuery1.Close;
  FDQuery1.SQL.Text := 'SELECT * FROM Kunden ORDER BY Nachname1, Vorname1, Gebdatum1';
  FDQuery1.AfterOpen := FDQuery1AfterOpen;
  FDQuery1.ResourceOptions.CmdExecMode := amAsync;
  FDQuery1.Open;
end;

end.
DFM:
Delphi-Quellcode:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 336
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 272
    Top = 40
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object DBGrid1: TDBGrid
    Left = 0
    Top = 71
    Width = 635
    Height = 265
    Align = alBottom
    DataSource = DataSource1
    TabOrder = 1
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'Tahoma'
    TitleFont.Style = []
  end
  object FDConnection1: TFDConnection
    Params.Strings = (
      'Database=Demo'
      'ServerTypes=Remote'
      'User_Name=Demo'
      'Password=Demo'
      'DriverID=ADS')
    Left = 464
    Top = 56
  end
  object FDQuery1: TFDQuery
    Connection = FDConnection1
    SQL.Strings = (
      'SELECT * FROM Kunden')
    Left = 456
    Top = 128
  end
  object DataSource1: TDataSource
    DataSet = FDQuery1
    Left = 456
    Top = 200
  end
end
  Mit Zitat antworten Zitat