refactor foregroundTimerCallback()

This commit is contained in:
Kang Yu 2024-12-07 11:47:32 -08:00
parent b519812fa5
commit 886182afc1

View file

@ -386,7 +386,7 @@ namespace PersistentWindows.Common
{
CleanupDisplayRegKeyCore(key);
}
catch(Exception ex)
catch (Exception ex)
{
Log.Error(ex.ToString());
}
@ -457,87 +457,7 @@ namespace PersistentWindows.Common
return false;
}
public bool Start(bool auto_restore_from_db, bool auto_restore_last_capture_at_startup)
{
process = Process.GetCurrentProcess();
processPriority = process.PriorityClass;
string productName = System.Windows.Forms.Application.ProductName;
appDataFolder = redirectAppDataFolder ? "." :
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), productName);
#if DEBUG
//avoid db path conflict with release version
//appDataFolder = ".";
appDataFolder = AppDomain.CurrentDomain.BaseDirectory;
#endif
try
{
string singletonLockName = $@"{appDataFolder}/{productName}.db.lock";
singletonLock = new LiteDatabase(singletonLockName);
}
catch (Exception)
{
User32.SetThreadDpiAwarenessContextSafe();
System.Windows.Forms.MessageBox.Show("Another instance is already running.", productName,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation,
System.Windows.Forms.MessageBoxDefaultButton.Button1,
System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly
);
return false;
}
var db_version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
persistDbName = $@"{appDataFolder}/{productName}.{db_version}.db";
bool found_latest_db_file_version = false;
if (File.Exists(persistDbName))
found_latest_db_file_version = true;
var dir_info = new DirectoryInfo(appDataFolder);
foreach (var file in dir_info.EnumerateFiles($@"{productName}*.db"))
{
var fname = file.Name;
if (found_latest_db_file_version && !fname.Contains(db_version))
{
// remove outdated db files
/*
try
{
file.Delete();
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
*/
}
else if (!found_latest_db_file_version)
{
//load outdated db
persistDbName = file.FullName;
break;
}
}
ReadDataDumpSafe();
curDisplayKey = GetDisplayKey();
CaptureNewDisplayConfig(curDisplayKey);
#if DEBUG
//TestSetWindowPos();
var debugTimer = new Timer(state =>
{
DebugInterval();
});
debugTimer.Change(2000, 2000);
#endif
foregroundTimer = new Timer(state =>
private void foregroundTimerCallback(object state)
{
IntPtr hwnd = foreGroundWindow;
@ -667,7 +587,89 @@ namespace PersistentWindows.Common
if (normalSessions.Contains(curDisplayKey))
CaptureApplicationsOnCurrentDisplays(curDisplayKey, immediateCapture:true);
}
}
public bool Start(bool auto_restore_from_db, bool auto_restore_last_capture_at_startup)
{
process = Process.GetCurrentProcess();
processPriority = process.PriorityClass;
string productName = System.Windows.Forms.Application.ProductName;
appDataFolder = redirectAppDataFolder ? "." :
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), productName);
#if DEBUG
//avoid db path conflict with release version
//appDataFolder = ".";
appDataFolder = AppDomain.CurrentDomain.BaseDirectory;
#endif
try
{
string singletonLockName = $@"{appDataFolder}/{productName}.db.lock";
singletonLock = new LiteDatabase(singletonLockName);
}
catch (Exception)
{
User32.SetThreadDpiAwarenessContextSafe();
System.Windows.Forms.MessageBox.Show("Another instance is already running.", productName,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation,
System.Windows.Forms.MessageBoxDefaultButton.Button1,
System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly
);
return false;
}
var db_version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
persistDbName = $@"{appDataFolder}/{productName}.{db_version}.db";
bool found_latest_db_file_version = false;
if (File.Exists(persistDbName))
found_latest_db_file_version = true;
var dir_info = new DirectoryInfo(appDataFolder);
foreach (var file in dir_info.EnumerateFiles($@"{productName}*.db"))
{
var fname = file.Name;
if (found_latest_db_file_version && !fname.Contains(db_version))
{
// remove outdated db files
/*
try
{
file.Delete();
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
*/
}
else if (!found_latest_db_file_version)
{
//load outdated db
persistDbName = file.FullName;
break;
}
}
ReadDataDumpSafe();
curDisplayKey = GetDisplayKey();
CaptureNewDisplayConfig(curDisplayKey);
#if DEBUG
//TestSetWindowPos();
var debugTimer = new Timer(state =>
{
DebugInterval();
});
debugTimer.Change(2000, 2000);
#endif
foregroundTimer = new Timer(foregroundTimerCallback);
captureTimer = new Timer(state =>
{