XE8-indy10中TIdThread.Execute函数的源码与解读

在XE8中,我们可以使用indy10中的线程控件来添加一个线程,可是我们从属性栏却找不到线程的Execute(如图),实际上OnRun就是Execute。

XE8-indy10中TIdThread.Execute函数的源码与解读_第1张图片


下面是源码解读,中文注释为自己写的

procedure TIdThread.Execute;
begin
  // Must make this call from INSIDE the thread. The call in Create
  // was naming the thread that was creating this thread. :(
  //
  // RLebeau - no need to put this inside the try blocks below as it
  // already uses its own try..except block internally
  if Name = '' then begin
    Name := 'IdThread (unknown)';
  end;
  SetThreadName(Name);		//设置线程名称

  {$IFDEF PLATFORM_CLEANUP_NEEDED}
    {$IFDEF MACOS}
  // Register the auto release pool
  FObjCPool := objc_msgSend(objc_msgSend(objc_getClass('NSAutoreleasePool'), sel_getUid('alloc')), sel_getUid('init'));
    {$ENDIF MACOS}
  {$ENDIF}

  try
    BeforeExecute;		//执行OnBeforeExecute函数
    try
      while not Terminated do begin	
        if Stopped then begin		
          DoStopped;		//如果Stoped置位,则执行OnStoped函数
          // It is possible that either in the DoStopped or from another thread,
          // the thread is restarted, in which case we dont want to restop it.
          if Stopped then begin                        
            if Terminated then begin
              Break;
            end;
            // Thread manager will revive us
            {$IFDEF DEPRECATED_TThread_SuspendResume}
            Suspended := True;
            {$ELSE}
            Suspend;		//挂起线程
            {$ENDIF}
            if Terminated then begin
              Break;
            end;
          end;
        end;

        Include(FOptions, itoReqCleanup);
        try
          try
            try
              BeforeRun;		//执行OnBeforeRun函数
              if Loop then begin	//判断是否循环执行
                while not Stopped do begin	//循环判断Stoped标志位,所以可以用Stop方法来停止线程运行
                  try
                    Run;		//执行OnRun函数
                  except
                    on E: Exception do begin		//捕捉异常,如果有异常则进入OnHandleRunException函数
                      if not HandleRunException(E) then begin
                        Terminate;			//结束进程
                        raise;				//抛出异常
                      end;
                    end;
                  end;
                end;
              end else begin		//如果不是循环执行
                try
                  Run;			//执行OnRun函数
                except
                  on E: Exception do begin
                    if not HandleRunException(E) then begin
                      Terminate;
                      raise;
                    end;
                  end;
                end;
              end;
            finally
              AfterRun;		//无论是否抛出异常,执行OnAfterRun函数
            end;
          except
            Terminate;
            raise;
          end;
        finally
          Cleanup;		//执行OnCleanup函数
        end;
      end;
    finally
      AfterExecute;		//执行OnAterExecute函数
    end;
  except
    on E: Exception do begin	//捕捉异常
      FTerminatingExceptionClass := E.ClassType;
      FTerminatingException := E.Message;
      DoException(E);		//执行OnException
      Terminate;
    end;
  end;
end;



注释翻译:

  // Must make this call from INSIDE the thread. The call in Create
  // was naming the thread that was creating this thread. :(
  //
  // RLebeau - no need to put this inside the try blocks below as it
  // already uses its own try..except block internally


第一句不懂。。。

第二句:

不需要在Execute里面写捕捉异常代码,因为已经有自己的try...except捕捉异常了。即Exception和Hand

你可能感兴趣的:(Delphi,indy10)