Delphi Thread
主窗体:
procedure TfrmMain.btnResumeClick(Sender: TObject);begin myThread := TClientThread.Create(LeftPart(cbbServer.Text, ':'), StrToInt(RightPart(cbbServer.Text, ':'))); myThread.Resume; mmoLog.Lines.Add('线程已激活');end;procedure TfrmMain.btnSuspendClick(Sender: TObject);begin if myThread = nil then begin ShowMessage('线程未创建!'); Exit; end; myThread.Suspend; mmoLog.Lines.Add('线程已挂起');end;procedure TfrmMain.btnStopClick(Sender: TObject);begin if myThread = nil then begin ShowMessage('线程未创建!'); Exit; end; myThread.Terminate; myThread.WaitFor; mmoLog.Lines.Add('线程已停止');end;unit uClientThread;interfaceuses Windows, Classes, SysUtils, IdTCPClient, uString, Main, uCommon, Messages, Dialogs;const CM_DIAL = WM_USER + $100; CM_HUNGUP = WM_USER + $101;type TClientThread = class(TThread) private FClient: TIdTCPClient; FRequest: TStrings; FResponse: TStrings; FNotifyHandle: HWND; // FPioneerCtrl: HWND; procedure DoDial; procedure DoDrop; procedure DoUpload; procedure DoSave; protected procedure Execute; override; procedure Insert(Index: Integer; const str: string); public constructor Create(const AHost: string; const APort: Integer); destructor Destroy; property NotifyHandle: HWND read FNotifyHandle write FNotifyHandle; end;implementationconstructor TClientThread.Create;begin inherited Create(True); FRequest := TStringList.Create; FResponse := TStringList.Create; FClient := TIdTCPClient.Create(nil); FClient.Host := AHost; FClient.Port := APort; FNotifyHandle := frmMain.mmoLog.Handle;end;procedure TClientThread.Execute;var Len : Integer; Cmd : string;begin FClient.Connect(6000); while not Terminated and FClient.Connected do begin try Len := 0; FRequest.Clear; FClient.ReadBuffer(Len, SizeOf(Len)); FRequest.Text := FClient.ReadString(Len); Cmd := FRequest.Values['Command']; if Cmd = 'Dial' then DoDial else if Cmd = 'Drop' then DoDrop else if Cmd = 'Upload' then DoUpload else if Cmd = 'Save' then DoSave; except end; end;end;procedure TClientThread.DoDial;begin Insert(frmMain.mmoLog.Lines.Count, FRequest.Text);end;procedure TClientThread.DoDrop;begin Insert(frmMain.mmoLog.Lines.Count, FRequest.Text);end;procedure TClientThread.DoUpload;begin Insert(frmMain.mmoLog.Lines.Count, FRequest.Text);end;procedure TClientThread.DoSave;begin Insert(frmMain.mmoLog.Lines.Count, FRequest.Text);end;procedure TClientThread.Insert(Index: Integer; const str: string);var SelStart, LineLen: Integer; Line: string;begin if Index >= 0 then begin SelStart := SendMessage(FNotifyHandle, EM_LINEINDEX, Index, 0); if SelStart >= 0 then Line := str + #13#10 else begin SelStart := SendMessage(FNotifyHandle, EM_LINEINDEX, Index - 1, 0); if SelStart < 0 then Exit; LineLen := SendMessage(FNotifyHandle, EM_LINELENGTH, SelStart, 0); if LineLen = 0 then Exit; Inc(SelStart, LineLen); Line := #13#10 + str; end; SendMessage(FNotifyHandle, EM_SETSEL, SelStart, SelStart); SendMessage(FNotifyHandle, EM_REPLACESEL, 0, Longint(PChar(Line))); end;end; destructor TClientThread.Destroy;begin FClient.DisconnectSocket; FClient.Free; FRequest.Free; FResponse.Free; inherited destroy;end;end.
if Cmd = 'Dial' then self.Synchronize(DoDial) else if Cmd = 'Drop' then self.Synchronize(DoDrop) else if Cmd = 'Upload' then self.Synchronize(DoUpload) else if Cmd = 'Save' then self.Synchronize(DoSave);