Many software we usually see will display which module that is currently starting in the startup screen, and gives a prompt when the module is loaded, this is the benefit of a professional software user knows the current software loading. Which modules are made, or when the software starts an error, let the user get feedback when the module started, and during the long software startup process, let the user know that the software is still working, avoiding the user to lose information. . .
Ok, I said so much nonsense, let's take a look at how I make such a program, because I usually use Delphi to develop, so the following code is also delphi, but the basic framework has, believe It will not be difficult to implement other languages. In addition, the following code is that I am refined in the group in the past development process, although it is not possible to reach the point of use, but there is not much content to be modified. .
My class is called TAPPLOADER. The first thing to do is to let it take over the initialization of some programs.
Write the startup code in the project DPR file:
Var gapploader: Tapploader;
Begin Application.initialize; gapploader: = tapploader.create (); trynesser.doload () Then Begin Application.run; end; finploader.free; end; end. It can be seen that all startup code is in Tapployer. Doload () function, if this function fails, it will return FALSE, and then skip Application.Run (); process, directly jump out of the program. Next, look at the definition of the class: TAppLoader = class (TObject) private FSplashForm: TfrmSplash; FManagerList: TList; protected procedure InitializeManager (var AManager; AManagerClass: TCustomManagerClass); procedure OnAppLoading (ASender: TObject; AEvent: String; ADelay : Integer = 5); public constructor create (); destRuctor destroy (); OVERRIDE; FUNCTION DOLOAD: BOOLEAN; END; In addition to the Doload () function, you can see such two functions: InitializationManager () and ONAppLoading (). You need to introduce such a class before explaining the initializationManager () function: TCUSTOMMANAGERCLASS = Class of TCUSTomManager;
TCustomManager = class (TObject) private FOnAppLoading: TAppLoadingEvent; protected procedure Initialize (); virtual; abstract; procedure Finalize (); virtual; abstract; procedure DoAppLoading (AEvent: String); property OnAppLoading: TAppLoadingEvent read FOnAppLoading write FOnAppLoading; public constructor Create (); Virtual; END; in my program, all global resource management classes are called TXXManager, while TCUSTomManager defines some of these basic behaviors. Speaking here, it may be necessary to explain what is the resource management class, it is white, that is, the entire software period needs to be accessed, and the functions used are all managed. For example, I will call the database connection: TDataManager, set the serial port Communication features are called: tcommmanager, and so on. . . This base class defines two virtual methods for initialize () and finalize (), which is used to start the service to start or turn off the service. These two methods are different from the constructors and destructor. They initialize the resources of the class itself. Instead, some external connection resources (such as network connection, file handle, serial port, etc.), which can be reconnected without destroying objects, that is, in addition to the initialize (), in addition to the TAPPLOADER. Finalize () method, you can also call these two methods during the use of the software, (such as the user chooses a new serial port port number). Next, you can see that there is an onapploading event in the TCustomManager. During the initialize () process, the actual Manager class can call the method, and the text is displayed on the startup screen. The actual event will be called TAppLoader.OnAppLoading () method, its code is as follows: procedure TAppLoader.OnAppLoading (ASender: TObject; AEvent: String; ADelay: Integer); begin if Assigned (FSplashForm) then begin if Assigned (ASender) then begin FSplashForm.lbl1.Caption: = ASender.ClassName ':' aEvent; end else begin FSplashForm.lbl1.Caption: = aEvent; end; FSplashForm.Update; if ADelay> 0 then Sleep (ADelay); end; end; wherein FSplashForm That is to start the picture, when calling each Manager's initialize () method in tapploader.doload (), these Manager calls this onapploading () function according to the current initialization content, at which time you can display the text on the startup screen. .
As long as the actual call to the Manager class DoAppLoading () method, the text can be displayed on the startup screen, such as: procedure TFileImageManager.Initialize (); var Directory: String; FindHandle: THandle; FindFileData: TWin32FindData; begin Directory: = ExtractFilePath (ParamStr (0)) 'decoders /'; FindHandle: = FindFirstFile (PChar (Directory '. * dcd'), FindFileData); if FindHandle = INVALID_HANDLE_VALUE then exit; repeat if (FindFileData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> FILE_ATTRIBUTE_DIRECTORY then begin DoAppLoading ( 'Loading' FindFileData.cFileName); AddDecoder (Directory FindFileData.cFileName); end; until not FindNextFile (FindHandle, FindFileData); Windows.FindClose (FindHandle); end; TAppLoader there is such a function: procedure TAppLoader.InitializeManager (var AManager; AManagerClass: TCustomManagerClass); var Instance: TCustomManager; begin Instance: = TCustomManager (AManagerClass.NewInstance); TCustomManager (AManager): = Instance; try Instance.Create (); FManagerList.Add (@AManager) Instance.onApploading: = onapploading; instance Ance.initialize (); instance.onapploading: = nil; Except tcustomManager (AMANAGER): = NIL; raise; end; end; it is used to start a manager and add it to a FManagerList list of TAPPLoader, in TAPPLoader destructure When it will automatically follow this list to release all of the Manager. After the manager's initialize (), compare insurance is to reset its onapploading, so if you call Manager's initialize () by other functions, it will not be called to display startup. Part of the text.
Finally, a look doload () function: function TAppLoader.DoLoad: Boolean; begin Result: = false; Application.Title: = 'Ultra Album'; FSplashForm: = TfrmSplash.Create (nil); try try FSplashForm.Show; OnAppLoading ( nil, 'Starting ...'); Sleep (100); InitializeManager (GOptionManager, TOptionManager); InitializeManager (GRdItemClassManager, TRdItemClassManager); InitializeManager (GImageManager, TFileImageManager); InitializeManager (GThemeManager, TFileThemeManager2); InitializeManager (GMaskManager, TFileMaskManager);
ONAppLoading (nil, 'ending ..., 0);
Application.createform; if paramcount> = 1 dam // deal with the filename in the parameter fsplashform.hide; frmmain.show; frmmain.doopenfile (paramstr (1)); end;
Result: = true; Except ON E: Exception Do Begin MessageBox (Application.Handle, Pchar (E.CLASSNAME ':' # 13 # 10 E.MESSAGE), PCHAR (Application.title), MB_ICONERROR); End; Finally FreeAndnil (FSPLASHFORM); END; END; This function is the code in my software, which constructs and displays a boot screen, then initialize 5 Manager class using INITIALIZEMANAGER (), where GoptionManager, GrdItemclassManager . . . They are all global objects. When they need to visit, they use this global object to access. Here I don't use Singleton mode, because I think these objects must fully initialize before the program main form is created, while Singleton's design The idea is to create its instance when the object is first used, and this function is not required in my use. Of course, you can also transform these Manager classes to become Singleton, the change code will not be much. Finally, create the main interface of the program, you can see that the creation code of this main interface is the line we deleted from the DPR file.