⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idthreadcomponent.pas

📁 photo.163.com 相册下载器 多线程下载
💻 PAS
📖 第 1 页 / 共 2 页
字号:

procedure TIdThreadComponent.DoBeforeRun;
begin
  if Assigned(FOnBeforeRun) then
  begin
    FOnBeforeRun(SELF);
  end;
end;

procedure TIdThreadComponent.DoCleanup;
begin
  if Assigned(FOnCleanup) then
  begin
    FOnCleanup(SELF);
  end;
end;

destructor TIdThreadComponent.Destroy;
begin
  {FThread.TerminateAndWaitFor;}
  //make sure thread is not active before we attempt to destroy it
  if Assigned(FThread) then begin
    FThread.Terminate;
    FThread.Start;//resume for terminate
  end;
  FreeAndNIL(FThread);
  inherited Destroy;
end;

procedure TIdThreadComponent.DoException(AThread: TIdThread; AException: Exception);
begin
  if Assigned(FOnException) then begin
    FOnException(SELF,AException);
  end;
end;

function TIdThreadComponent.DoHandleRunException(AException: Exception): Boolean;
begin
  Result := FALSE;//not handled
  if Assigned(FOnHandleRunException) then begin
    FOnHandleRunException(SELF,AException,Result);
  end;
end;

procedure TIdThreadComponent.DoStopped(AThread: TIdThread);
begin
  if Assigned(FOnStopped) then begin
    FOnStopped(SELF);
  end;
end;

procedure TIdThreadComponent.DoTerminate;
begin
  if Assigned(FOnTerminate) then begin
    FOnTerminate(SELF);
  end;
end;

function TIdThreadComponent.GetData: TObject;
begin
  Result := FThread.Data;
end;

function TIdThreadComponent.GetHandle: THandle;
begin
  Result := GetThreadHandle(FThread);
end;

function TIdThreadComponent.GetReturnValue: Integer;
begin
  Result := FThread.ReturnValue;
end;

function TIdThreadComponent.GetStopMode: TIdThreadStopMode;
begin
  if FThread = NIL then begin
    Result := FStopMode;
  end else begin
    Result := FThread.StopMode;
  end;
end;

function TIdThreadComponent.GetStopped: Boolean;
begin
  if Assigned(FThread) then begin
    Result := FThread.Stopped;
  end else begin
    Result := TRUE;
  end;
end;

function TIdThreadComponent.GetSuspended: Boolean;
begin
  Result := FThread.Suspended;
end;

function TIdThreadComponent.GetTerminated: Boolean;
begin
  if Assigned(FThread) then begin
    Result := FThread.Terminated;
  end else begin
    Result := TRUE;
  end;
end;

function TIdThreadComponent.GetTerminatingException: string;
begin
  Result := FThread.TerminatingException;
end;

function TIdThreadComponent.GetTerminatingExceptionClass: TClass;
begin
  Result := FThread.TerminatingExceptionClass;
end;

procedure TIdThreadComponent.Loaded;
begin
  inherited Loaded;
  // Active = True must not be performed before all other props are loaded
  if Assigned(OnTerminate) then begin
    FThread.OnTerminate := DoTerminate;
  end;

  if FActive then begin
    // Retoggle for load since we ignore during loading until all properties
    // are ready
    FActive := False;
    Active := True;
  end;
end;

procedure TIdThreadComponent.DoRun;
begin
  if Assigned(FOnRun) then begin
    FOnRun(SELF);
  end;
end;

procedure TIdThreadComponent.SetActive(const AValue: Boolean);
begin
  if ((csDesigning in ComponentState) = False)
   and ((csLoading in ComponentState) = False) then begin
    if Active <> AValue then begin
      if AValue then begin
        Start;
      end else begin
        Stop;
      end;
    end;
  end;
  FActive := AValue;
end;

procedure TIdThreadComponent.SetData(const AValue: TObject);
begin
// this should not be accessed at design-time.
  FThread.Data := AValue;
end;

procedure TIdThreadComponent.SetReturnValue(const AValue: Integer);
begin
// this should not be accessed at design-time.
  FThread.ReturnValue := AValue;
end;

procedure TIdThreadComponent.SetStopMode(const AValue: TIdThreadStopMode);
begin
  if Assigned(FThread) and NOT FThread.Terminated then begin
    FThread.StopMode := AValue;
  end;
  FStopMode := AValue;
end;

procedure TIdThreadComponent.Start;
begin
  if NOT (csDesigning in ComponentState) then begin
    if Assigned(FThread) and FThread.Terminated then begin
      FreeAndNIL(FThread);
    end;

    if NOT Assigned(FThread) then begin
      FThread := TIdThreadEx.Create(SELF);
    end;

    // MUST read from F variants as thread is now created
    FThread.Name := FThreadName;
    FThread.Loop := FLoop;
    FThread.Priority := FPriority;
    FThread.StopMode := FStopMode;
    FThread.Start;
  end;
end;

procedure TIdThreadComponent.Stop;
begin
  if Assigned(FThread) then begin
    FThread.Stop;
  end;
end;

procedure TIdThreadComponent.Synchronize(AMethod: TThreadMethod);
begin
  FThread.Synchronize(AMethod);
end;

procedure TIdThreadComponent.Terminate;
begin
  FThread.Terminate;
end;

procedure TIdThreadComponent.TerminateAndWaitFor;
begin
  FThread.TerminateAndWaitFor;
end;

function TIdThreadComponent.WaitFor: LongWord;
begin
  Result := FThread.WaitFor;
end;

function TIdThreadComponent.GetPriority: TIdThreadPriority;
begin
  if FThread <> nil then begin
    Result := FThread.Priority;
  end else begin
    Result := FPriority;
  end;
end;

procedure TIdThreadComponent.SetPriority(const AValue: TIdThreadPriority);
begin
  if Assigned(FThread) then begin
    if not FThread.Terminated then begin
      FThread.Priority := AValue;
    end;
  end;
  FPriority := AValue;
end;

function TIdThreadComponent.GetActive: Boolean;
begin
  Result := False;
  if csDesigning in ComponentState then begin
    Result := FActive;
  end else if FThread <> nil then begin
    Result := IsRunning;
  end;
end;

procedure TIdThreadComponent.SetOnTerminate(const AValue: TIdNotifyThreadComponentEvent);
begin
  FOnTerminate := AValue;
  if Assigned(FThread) then begin
    if Assigned(AValue) then begin
      FThread.OnTerminate := DoTerminate;
    end else begin
      FThread.OnTerminate := nil;
    end;
  end;
end;

procedure TIdThreadComponent.SetLoop(const AValue: Boolean);
begin
  EIdException.IfTrue(IsRunning, RSThreadComponentLoopAlreadyRunning);
  FLoop := AValue;
end;

procedure TIdThreadComponent.SetThreadName(const AValue: string);
begin
  EIdException.IfTrue(IsRunning, RSThreadComponentThreadNameAlreadyRunning);
  FThreadName := AValue;
end;

function TIdThreadComponent.IsRunning: Boolean;
begin
  if FThread = nil then begin
    Result := False;
  end else begin
    Result := not FThread.Stopped
  end;
end;

procedure TIdThreadComponent.InitComponent;
begin
  inherited;
  StopMode := IdThreadComponentDefaultStopMode;
  Priority := IdThreadComponentDefaultPriority;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -