Skip to content

Commit

Permalink
improved MonitorThread behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Lesueur Benjamin committed Feb 12, 2021
1 parent d3e952c commit aa99170
Showing 1 changed file with 49 additions and 39 deletions.
88 changes: 49 additions & 39 deletions DockerForm/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public partial class Form1 : Form
public static bool DockStatus = false;
public static bool IsFirstBoot = true;
public static bool IsHardwareNew = false;
public static bool IsHardwarePending = false;
public static bool IsHardwarePending = true;
public static bool IsPowerPending = true;
public static VideoController CurrentController;
public static bool prevPowerStatus;
public static bool PowerStatus;
Expand All @@ -43,6 +44,7 @@ public partial class Form1 : Form
public static bool SaveOnExit = false;
public static int IGDBListLength;
public static StringCollection Blacklist;
public static int MonitorThreadRefresh;

// Devices vars
public static Dictionary<Type, VideoController> VideoControllers = new Dictionary<Type, VideoController>();
Expand Down Expand Up @@ -93,7 +95,7 @@ private void OnPowerModeChanged(object s, PowerModeChangedEventArgs e)
break;
}

IsHardwarePending = true;
IsPowerPending = true;
}

private static void CheckPowerProfiles()
Expand Down Expand Up @@ -310,8 +312,7 @@ public static void MonitorThread(object data)
while(IsRunning)
{
bool IsGameRunning = GameProcesses.Count != 0;

if ((IsHardwarePending || IsFirstBoot) && !IsGameRunning)
if (IsHardwarePending && !IsGameRunning)
{
DateTime currentCheck = DateTime.Now;
VideoControllers.Clear();
Expand Down Expand Up @@ -342,52 +343,56 @@ public static void MonitorThread(object data)
// update all status
DockStatus = VideoControllers.ContainsKey(Type.Discrete);
CurrentController = DockStatus ? VideoControllers[Type.Discrete] : VideoControllers[Type.Internal];
PowerStatus = SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online;

// monitor hardware changes
IsHardwareNew = IsFirstBoot ? false : (prevDockStatus != DockStatus);
IsPowerNew = IsFirstBoot ? false : (prevPowerStatus != PowerStatus);

if (IsHardwareNew || IsPowerNew || IsFirstBoot)
// hardware has changed
if (IsHardwareNew)
{
// Video Controllers has changed
if (IsHardwareNew)
{
if (VideoControllers.ContainsKey(Type.Discrete))
SendNotification(VideoControllers[Type.Discrete].Name + " detected.", true, true);
else if (VideoControllers.ContainsKey(Type.Internal))
SendNotification(VideoControllers[Type.Internal].Name + " detected.", true, true);
}
if (VideoControllers.ContainsKey(Type.Discrete))
SendNotification(VideoControllers[Type.Discrete].Name + " detected.", true, true);
else if (VideoControllers.ContainsKey(Type.Internal))
SendNotification(VideoControllers[Type.Internal].Name + " detected.", true, true);

// Power Status has changed
if (IsPowerNew)
{
LogManager.UpdateLog("Power Status: " + GetCurrentPower());
SendNotification("Device is " + GetCurrentPower() + ".", true);
}
DatabaseManager.UpdateFilesAndRegistries(false, true);
DatabaseManager.UpdateFilesAndRegistries(true, false);
}

// Software is initializing
if (IsFirstBoot)
{
IsFirstBoot = false;
DatabaseManager.SanityCheck();
}
else
{
DatabaseManager.UpdateFilesAndRegistries(false, true);
DatabaseManager.UpdateFilesAndRegistries(true, false);
}
// update status
prevDockStatus = DockStatus;

// update form
UpdateFormIcons();
IsHardwarePending = false;
}

if (IsPowerPending)
{
PowerStatus = SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online;
IsPowerNew = IsFirstBoot ? false : (prevPowerStatus != PowerStatus);

// Power Status has changed
if (IsPowerNew)
{
LogManager.UpdateLog("Power Status: " + GetCurrentPower());
SendNotification("Device is " + GetCurrentPower() + ".", true);

CheckPowerProfiles();
}

// update status
prevDockStatus = DockStatus;
prevPowerStatus = PowerStatus;

// update form
UpdateFormIcons();
IsHardwarePending = false;
IsPowerPending = false;
}

if (IsFirstBoot)
{
DatabaseManager.SanityCheck();
CheckPowerProfiles();
}

Thread.Sleep(MonitorThreadRefresh);
Expand All @@ -400,18 +405,22 @@ public static void UpdateFormIcons()
{
// taskbar icon
Image ConstructorLogo = Properties.Resources.intel;
switch(CurrentController.Constructor)
string ConstructorName = CurrentController != null ? CurrentController.Name : "Unknown";
if (CurrentController != null)
{
case Constructor.AMD: ConstructorLogo = Properties.Resources.amd; break;
case Constructor.Nvidia: ConstructorLogo = Properties.Resources.nvidia; break;
switch (CurrentController.Constructor)
{
case Constructor.AMD: ConstructorLogo = Properties.Resources.amd; break;
case Constructor.Nvidia: ConstructorLogo = Properties.Resources.nvidia; break;
}
}
// main application icon
Icon myIcon = DockStatus ? Properties.Resources.tb3_on : Properties.Resources.tb3_off;

// drawing
_instance.BeginInvoke((MethodInvoker)delegate ()
{
_instance.menuStrip2.Items[0].Text = CurrentController.Name + " (" + GetCurrentPower() + ")";
_instance.menuStrip2.Items[0].Text = ConstructorName + " (" + GetCurrentPower() + ")";
_instance.undockedToolStripMenuItem.Image = ConstructorLogo;
_instance.notifyIcon1.Icon = myIcon;
_instance.Icon = myIcon;
Expand Down Expand Up @@ -567,6 +576,7 @@ public Form1()
ToastNotifications = Properties.Settings.Default.ToastNotifications;
SaveOnExit = Properties.Settings.Default.SaveOnExit;
Blacklist = Properties.Settings.Default.Blacklist;
MonitorThreadRefresh = Properties.Settings.Default.MonitorThreadRefresh;

if (MinimizeOnStartup)
{
Expand Down Expand Up @@ -644,7 +654,7 @@ private string GetProcessorID()
private void Form1_Shown(object sender, System.EventArgs e)
{
// search for GPUs
ThreadGPU = new Thread(VideoControllerMonitor);
ThreadGPU = new Thread(MonitorThread);
ThreadGPU.Start();

// Monitor processes
Expand Down

0 comments on commit aa99170

Please sign in to comment.