我在Application.run之前執行applicaton.terminater時,明顯得發現它不是馬上退出,而是等applicaton.run前面所有的初始化語句全部執行完了後,才退出,請問application.run到底起了什麼作用?

解决方案 »

  1.   

    看VCL代码阿procedure TApplication.Terminate;
    begin
      if CallTerminateProcs then PostQuitMessage(0);
    end;procedure TApplication.Run;
    begin
      FRunning := True;
      try
        AddExitProc(DoneApplication);
        if FMainForm <> nil then
        begin
          case CmdShow of
            SW_SHOWMINNOACTIVE: FMainForm.FWindowState := wsMinimized;
            SW_SHOWMAXIMIZED: MainForm.WindowState := wsMaximized;
          end;
          if FShowMainForm then
            if FMainForm.FWindowState = wsMinimized then
              Minimize else
              FMainForm.Visible := True;
          repeat
            try
              HandleMessage;
            except
              HandleException(Self);
            end;
          until Terminated;
        end;
      finally
        FRunning := False;
      end;
    end;function TApplication.ProcessMessage(var Msg: TMsg): Boolean;
    var
      Handled: Boolean;
    begin
      Result := False;
      if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
      begin
        Result := True;
        if Msg.Message <> WM_QUIT then
        begin
          Handled := False;
          if Assigned(FOnMessage) then FOnMessage(Msg, Handled);
          if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and
            not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then
          begin
            TranslateMessage(Msg);
            DispatchMessage(Msg);
          end;
        end
        else
          FTerminate := True;
      end;
    end;调用Terminate,会发送一个WM_QUIT消息,而ProcessMessage会在受到WM_QUIT后将FTerminate 设成True,从而结束Run的循环
    Run的作用主要就是这句HandleMessageprocedure TApplication.HandleMessage;
    var
      Msg: TMsg;
    begin
      if not ProcessMessage(Msg) then Idle(Msg);
    end;可以这样一直看下去归根结底一句话把VCL要处理的消息交给VCL处理,把系统要操作的消息交给操作系统处理,把用户的消息交给用户处理
      

  2.   

    它执行后,程序进入消息循环,run才是一个程序生命周期的开始
    李维的《Inside VCL》讲的很详细
      

  3.   

    你在调试的时候,有没有注意到,到工程文件还没有运行到application.run时,系统所有的为inaccessible value,但运行application.run后,系统才正式运行。在这里我认为在运行application.run之前,系统只是在检查语法错误等等之类。
      

  4.   

    在运行application.run之前,系统只是在检查语法错误等等之类。
    错!