Delphi Flow с Инди - PullRequest
       25

Delphi Flow с Инди

0 голосов
/ 11 декабря 2018

Программа зависает как обычно.как делать в потоке?

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdCookieManager, IdIOHandler, IdIOHandlerSocket,
  IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdHTTP, ExtCtrls, Unit2;

type
  TForm1 = class(TForm)
    IdHTTP1: TIdHTTP;
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
    IdCookieManager1: TIdCookieManager;
    Button1: TButton;
    Memo1: TMemo;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
  private
    my:myth;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  my:=myth.Create(true);
  my.Priority:=tpNormal;
  my.FreeOnTerminate:=True;
  my.Resume;
end;

end.

FLOW

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdCookieManager, IdIOHandler, IdIOHandlerSocket,
  IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdHTTP, ExtCtrls;

type
  myth = class(TThread)
    IdHTTP1: TIdHTTP;
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
    IdCookieManager1: TIdCookieManager;
    Button1: TButton;
    Memo1: TMemo;
    Timer1: TTimer;
  private
    { Private declarations }
  protected
    procedure Execute; override;
    procedure meme;
    public
  end;

implementation

uses Unit1;

procedure myth.Execute;
begin
  Synchronize(meme);
end;

procedure myth.meme;
var
  s: string;
  list, lista: TStringList;
  resul: string;
begin
  list := TStringList.Create;
  Form1.Memo1.Clear;
  list.Add('...');
  list.Add('...');
  list.Add('...');
  list.Add('...');
  s := IdHTTP1.Post('https://,list);
  list.Free;
(LOGIN)
  resul := idHTTP1.Get('...');
  while Pos('tdn',resul) > 0 do begin //PRESS ON BUTTON
    lista := TStringList.Create;
    lista.Add('...');
    IdHTTP1.Post('https:...,lista);
    lista.Free;
  end;
end;

end.

1 Ответ

0 голосов
/ 11 декабря 2018

Вы создаете рабочий поток только для Synchronize() всей своей работы обратно в основной поток пользовательского интерфейса.Не делай этого!Синхронизируйте только те части, которые действительно нужны.Пусть поток вызовет meme() напрямую без Synchronize(), а затем meme() использует Synchronize() для доступа к Form1.Memo1 и всему остальному, что касается пользовательского интерфейса.Сами ваши операции TStringList и TIdHTTP не нужно синхронизировать, так как они локальны для meme() (если вы создаете объекты IdHTTP1, IdSSLIOHandlerSocketOpenSSL1 и IdCookieManager1 динамически в потоке, а нев форме во время разработки).

Вместо этого попробуйте что-нибудь подобное:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Unit2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
  private
    my: myth;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  my := myth.Create;
end;

end.

FLOW

unit Unit2;

interface

uses
  Classes, IdCookieManager, IdSSLOpenSSL, IdHTTP;

type
  myth = class(TThread)
    IdHTTP: TIdHTTP;
    IdSSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL;
    IdCookieManager: TIdCookieManager;
  private
    { Private declarations }
    procedure meme;
  protected
    procedure Execute; override;
  public
    constructor Create; reintroduce;
    destructor Destroy; override;
  end;

implementation

uses Unit1;

constructor myth.Create;
begin
  inherited Create(False);
  Priority := tpNormal;
  FreeOnTerminate := True;

  IdHTTP := TIdHTTP.Create(nil);

  IdSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
  // configure as needed...
  IdHTTP.IOHandler := IdSSLIOHandlerSocketOpenSSL;

  IdCookieManager := TIdCookieManager.Create(IdHTTP);
  // configure as needed...
  IdHTTP.CookieManager := IdCookieManager;
end;

destructor myth.Destroy;
begin
  IdHTTP.Free;
  inherited;
end;

procedure myth.Execute;
begin
  meme;
end;

procedure myth.meme;
var
  list: TStringList;
  resul: string;
begin
  list := TStringList.Create;
  try
    Synchronize(ClearMemo);
    list.Add('...');
    list.Add('...');
    list.Add('...');
    list.Add('...');
    s := IdHTTP1.Post('https://...', list);
    list.Clear;
    ...
    resul := IdHTTP1.Get('...');
    while Pos('tdn', resul) > 0 do begin
      list.Clear;
      list.Add('...');
      IdHTTP1.Post('https://...', list);
      list.Clear;
    end;
  finally
    list.Free;
  end;
end;

procedure myth.ClearMemo;
begin
  Form1.Memo1.Clear;
end;

end.
...