From a2ef1aeccdcb705f893f33166e98f96cf71e2394 Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Sat, 13 Apr 2019 10:43:52 +0200 Subject: [PATCH] Issue #140: Store application process id in each tabs.ini section, and don't restore tabs from other running processes. If the process no longer runs, restore such tab sections again, so nothing gets lost with multiple running application instances. --- source/apphelpers.pas | 21 +++++++++++++++++++++ source/main.pas | 21 ++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/source/apphelpers.pas b/source/apphelpers.pas index 7c4da523..426b3baa 100644 --- a/source/apphelpers.pas +++ b/source/apphelpers.pas @@ -359,6 +359,7 @@ type function DpiScaleFactor(Form: TForm): Double; function GetThemeColor(Color: TColor): TColor; function ThemeIsDark(ThemeName: String): Boolean; + function ProcessExists(pid: Cardinal): Boolean; var AppSettings: TAppSettings; @@ -3010,6 +3011,26 @@ begin end; +function ProcessExists(pid: Cardinal): Boolean; +var + Proc: TProcessEntry32; + SnapShot: THandle; + ContinueLoop: Boolean; +begin + // Check if a given process id exists + SnapShot := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0); + Proc.dwSize := Sizeof(Proc); + Result := False; + ContinueLoop := Process32First(SnapShot, Proc); + while ContinueLoop do begin + Result := Proc.th32ProcessID = pid; + if Result then + Break; + ContinueLoop := Process32Next(SnapShot, Proc); + end; + CloseHandle(Snapshot); +end; + { Threading stuff } diff --git a/source/main.pas b/source/main.pas index 36b00351..efed74e2 100644 --- a/source/main.pas +++ b/source/main.pas @@ -2173,11 +2173,13 @@ begin Section := Tab.Uid; if Tab.Memo.GetTextLen > 0 then begin - // Avoid writing the tabs.ini file through WriteString if nothing was effectively changed + // Avoid writing the tabs.ini file if nothing was effectively changed if TabsIni.ReadString(Section, 'BackupFilename', '') <> Tab.MemoBackupFilename then TabsIni.WriteString(Section, 'BackupFilename', Tab.MemoBackupFilename); if TabsIni.ReadString(Section, 'Filename', '') <> Tab.MemoFilename then TabsIni.WriteString(Section, 'Filename', Tab.MemoFilename); + if TabsIni.ReadInteger(Section, 'pid', 0) <> Integer(GetCurrentProcessId) then + TabsIni.WriteInteger(Section, 'pid', Integer(GetCurrentProcessId)); end; end; @@ -2197,6 +2199,7 @@ var Sections: TStringList; Section, Filename, BackupFilename: String; TabsIni: TIniFile; + pid: Cardinal; begin // Restore query tab setup from tabs.ini @@ -2206,9 +2209,19 @@ begin Sections := TStringList.Create; TabsIni.ReadSections(Sections); + for Section in Sections do begin + Filename := TabsIni.ReadString(Section, 'Filename', ''); BackupFilename := TabsIni.ReadString(Section, 'BackupFilename', ''); + pid := Cardinal(TabsIni.ReadInteger(Section, 'pid', 0)); + + // Don't restore this tab if it belongs to a different running process + if (pid > 0) and (pid <> GetCurrentProcessId) and ProcessExists(pid) then + Continue; + + // Either we have a backup file, or a user stored file. + // Both of them may not exist. if not BackupFilename.IsEmpty then begin if FileExists(BackupFilename) then begin Tab := ActiveOrEmptyQueryTab(False); @@ -2217,7 +2230,7 @@ begin Tab.MemoFilename := Filename; Tab.Memo.Modified := True; end else begin - // Remove ini item if file is gone + // Remove tab section if backup file is gone or inaccessible for some reason TabsIni.EraseSection(Section); end; end else if not Filename.IsEmpty then begin @@ -2227,11 +2240,13 @@ begin Tab.LoadContents(Filename, True, nil); Tab.MemoFilename := Filename; end else begin - // Remove ini item if file is gone + // Remove tab section if user stored file was deleted by user TabsIni.EraseSection(Section); end; end; + end; + Sections.Free; // Close file TabsIni.Free;