PowerControl: Improve null handling

This commit is contained in:
Kamil Trzciński 2022-12-19 22:52:23 +01:00
parent a30ef2c400
commit 1ff7bed567
6 changed files with 19 additions and 14 deletions

View file

@ -105,7 +105,7 @@ namespace CommonHelpers
}
}
public static T WithGlobalMutex<T>(int timeoutMs, Func<T> func)
public static T? WithGlobalMutex<T>(int timeoutMs, Func<T?> func)
{
var mutex = WaitGlobalMutex(timeoutMs);
if (mutex is null)

View file

@ -10,12 +10,13 @@ namespace CommonHelpers
return IsOSDForeground(out _);
}
public static bool IsOSDForeground(out int? processId)
public static bool IsOSDForeground(out int processId)
{
try
{
processId = (int?)GetTopLevelProcessId();
if (processId is null)
var id = GetTopLevelProcessId();
processId = (int)id.GetValueOrDefault(0);
if (id is null)
return false;
foreach (var app in OSD.GetAppEntries(AppFlags.MASK))
@ -28,7 +29,7 @@ namespace CommonHelpers
}
catch
{
processId = null;
processId = 0;
return false;
}
}

View file

@ -8,10 +8,10 @@ namespace PowerControl.Menu
public int ApplyDelay { get; set; }
public bool CycleOptions { get; set; } = true;
public Func<object>? CurrentValue { get; set; }
public Func<object[]>? OptionsValues { get; set; }
public Func<object, object>? ApplyValue { get; set; }
public Func<object>? ResetValue { get; set; }
public Func<object?>? CurrentValue { get; set; }
public Func<object[]?>? OptionsValues { get; set; }
public Func<object, object?>? ApplyValue { get; set; }
public Func<object?>? ResetValue { get; set; }
private System.Windows.Forms.Timer delayTimer = new System.Windows.Forms.Timer();
private ToolStripMenuItem toolStripItem = new ToolStripMenuItem();

View file

@ -34,7 +34,7 @@ namespace PowerControl.Options
return selected;
}
sd.HardMinGfxClock = uint.Parse(selected.ToString().Replace("MHz", ""));
sd.HardMinGfxClock = uint.Parse(selected.ToString()?.Replace("MHz", "") ?? "200");
return selected;
}
});

View file

@ -18,7 +18,7 @@ namespace PowerControl.Options
if (!ProcessorCores.HasSMTThreads())
return null;
return ProcessorCores.IsUsingSMT(processId.Value) ? "Yes" : "No";
return ProcessorCores.IsUsingSMT(processId) ? "Yes" : "No";
},
ApplyValue = delegate (object selected)
{
@ -27,9 +27,9 @@ namespace PowerControl.Options
if (!ProcessorCores.HasSMTThreads())
return null;
ProcessorCores.SetProcessSMT(processId.Value, selected.ToString() == "Yes");
ProcessorCores.SetProcessSMT(processId, selected.ToString() == "Yes");
return ProcessorCores.IsUsingSMT(processId.Value) ? "Yes" : "No";
return ProcessorCores.IsUsingSMT(processId) ? "Yes" : "No";
}
};
}

View file

@ -21,7 +21,11 @@ namespace PowerControl.Options
)
return null;
uint mW = uint.Parse(selected.ToString().Replace("W", "")) * 1000;
var selectedText = selected.ToString();
if (selectedText is null)
return null;
uint mW = uint.Parse(selectedText.Replace("W", "")) * 1000;
if (VangoghGPU.IsSupported)
{