Displaying splash screen in Delphi when main thread is busy

前端 未结 6 1016
难免孤独
难免孤独 2020-12-16 00:37

I\'d like to display splash screen while the application is loading. However some 3rd party components block main thread during initilization for several seconds, which caus

6条回答
  •  长情又很酷
    2020-12-16 00:52

    Create you splash screen in the DPR first, but don't use the Application.CreateForm method for it. Here is some simple code:

    begin
      Application.Initialize;
      SplashForm := TSplashForm.Create(nil);
      try
        SplashForm.FormStyle := fsStayOnTop;
        SplashForm.Show;
        Application.ProcessMessages;
        Application.CreateForm(TForm14, Form14);
        // Other Form Creation here . . . .
        Application.Run;
      finally
        if assigned(SplashForm) then
          SplashForm.Release;
      end;
    end.
    

    Then place the following code in the Show event handler (or later - when your initialization is done) for your MainFrom (in this case Form14):

    SplashForm.Close;
    SplashForm.Release;
    SplashForm := nil;
    

    (You call Release on a form instead of Free, and you assign it to nil so that the DRP doesn't call release again. The release in the DRP is just in case your mainform fails to create.)

    Since your splash form is FormStyle := fsStayOnTop it shouldn't be an issue that it isn't getting paint messages when your main thread blocks. Then when the main thread unblocks you send it an update message (to change the progress bar, etc.) Although I agree with Gamecat that you might want to contact your 3rd party component vendors and get them to stop blocking the main thread on you.

    Alternatively you could create your 3rd party components in a separate thread (provided they aren't visual, as that would be a little more difficult.)

    This will work with Application.MainFormOnTaskBar set to true as well.

提交回复
热议问题