delphiマルチスレッドタイミング実行プログラムの書き込み方法

1298 ワード

転載先:https://blog.csdn.net/chinajobs/article/details/51461368
unit Unit1;

interface

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

type
  TSysChatMainForm = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FTestNetThread: TThread;
  public
    { Public declarations }
  end;

var
  SysChatMainForm: TSysChatMainForm;

implementation

{$R *.dfm}

type
  TTestNetThread = class(TTHread)
    procedure Execute; override;
    procedure RefreshUIInMainThread;
  end;

{ TTestNetThread }

procedure TTestNetThread.RefreshUIInMainThread;
begin
  bsTrayIcon.ShowBalloonHint(' ',' , !',bsbitInfo);
end;

procedure TTestNetThread.Execute;
begin
  inherited;
  FreeOnTerminate := True;

  while (not Terminated) and (not InternetGetConnectedState(nil, 0)) then
  begin
    Data.UserOff('[ ]',false);
    Synchronize(RefreshUIInMainThread);

    Sleep(3000)
  end;
end;

{ TSysChatMainForm }

procedure TSysChatMainForm.FormCreate(Sender: TObject);
begin
  FTestNetThread := TTestNetThread.Create(False);
end;

procedure TSysChatMainForm.FormDestroy(Sender: TObject);
begin
  FTestNetThread.Terminate;
end;

end.