Skip to content

Commit 9243f1c

Browse files
Added ability to apply profile multiple times
You need to go into the Profile Settings to set the number of times that the profile is applied, and how long the delay should be in seconds before it is reapplied each time. Fixes #316 (well it provides a workaround for #316). Note - The target display profile you have selected in the game shortcut will be applied as per the number of times that it is set in the target display profile, but the profile that is reverted back to will only ever be applied once! This is because the display profile that you start with is a temporary display profile that is only stored whilst the game is being run. It is discarded when the game shortcut has finished processeing and everything is reverted back. A second change I made was to extend the default timeout for waiting for a game or exe to start to be 60 seconds. It hwas fairly common that a game would take too long to start, and it would timeout and DM would revert the display, only for the game to then finish starting (but the display profile would have reverted back). Extending the timeout fixes that issue. Lastly, a few Task.Delays were incorrectly being used, as they were not pausing properly. I added the .Wait(cancelToken) to them so that they would properly delay, and yet would be cancellable so that the user could cancel if they wanted.
1 parent 1280687 commit 9243f1c

File tree

9 files changed

+113
-47
lines changed

9 files changed

+113
-47
lines changed

DisplayMagician/ShortcutItem.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public class ShortcutItem : IComparable
164164
private string _gameAppId = "";
165165
private string _gameName = "";
166166
private SupportedGameLibraryType _gameLibrary = SupportedGameLibraryType.Unknown;
167-
private int _startTimeout = 20;
167+
private int _startTimeout = 60;
168168
private string _gameArguments = "";
169169
private bool _gameArgumentsRequired = false;
170170
private string _differentGameExeToMonitor = "";
@@ -573,7 +573,7 @@ public Keys Hotkey
573573
}
574574
}
575575

576-
[DefaultValue(20)]
576+
[DefaultValue(60)]
577577
public int StartTimeout
578578
{
579579
get
@@ -1017,7 +1017,7 @@ public void UpdateNoGameShortcut(
10171017
_monitorDifferentGameExe = false;
10181018
_differentGameExeToMonitor = "";
10191019
_processPriority = ProcessPriority.Normal;
1020-
_startTimeout = 20;
1020+
_startTimeout = 60;
10211021

10221022
ReplaceShortcutIconInCache();
10231023
RefreshValidity();

DisplayMagician/ShortcutRepository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,8 +1352,8 @@ public static RunShortcutResult RunShortcut(ShortcutItem shortcutToUse, ref Canc
13521352
logger.Error(ex, $"ShortcutRepository/RunShortcut: Exception caused whilst starting UWP App {appToUse.Name}.");
13531353
}
13541354

1355-
// Wait an extra few seconds to give the application time to settle down
1356-
//Thread.Sleep(2000);
1355+
// Wait an extra 2 seconds to give the application time to settle down
1356+
//Task.Delay(2000).Wait(cancelToken);
13571357

13581358
// Now we need to decide what we are monitoring. If the user has supplied an alternative process to monitor, then we monitor that instead!
13591359
bool foundSomethingToMonitor = false;
@@ -1443,8 +1443,8 @@ public static RunShortcutResult RunShortcut(ShortcutItem shortcutToUse, ref Canc
14431443
// We use the a user supplied executable as the thing we're monitoring instead!
14441444
try
14451445
{
1446-
// Wait 3 seconds for the different executable to start up. If there is a loader involved we want to give it some time.
1447-
Task.Delay(3000);
1446+
// Wait 10 seconds for the different executable to start up. If there is a loader involved we want to give it a good amount of time to load.
1447+
Task.Delay(10000).Wait(cancelToken);
14481448
processesToMonitor.AddRange(Process.GetProcessesByName(ProcessUtils.GetProcessName(shortcutToUse.DifferentExecutableToMonitor)));
14491449
if (processesToMonitor.Count > 0)
14501450
{

DisplayMagician/UIForms/ProfileSettingsForm.Designer.cs

Lines changed: 37 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DisplayMagician/UIForms/ProfileSettingsForm.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,36 @@ private void ProfileSettingsForm_Load(object sender, EventArgs e)
9191
cmb_wallpaper_display_mode.SelectedIndex = 0;
9292
}
9393

94+
if (Profile.ApplyProfileCount >= 0 && Profile.ApplyProfileCount <= 10)
95+
{
96+
nud_apply_profile_count.Value = Profile.ApplyProfileCount;
97+
}
98+
else
99+
{
100+
nud_apply_profile_count.Value = 1;
101+
}
102+
103+
if (Profile.ApplyProfileDelay >= 0 && Profile.ApplyProfileDelay <= 1000)
104+
{
105+
nud_apply_profile_delay.Value = Profile.ApplyProfileDelay;
106+
}
107+
else
108+
{
109+
nud_apply_profile_delay.Value = 0;
110+
}
111+
112+
if (nud_apply_profile_count.Value > 1)
113+
{
114+
lbl_apply_profile_delay.Visible = true;
115+
nud_apply_profile_delay.Visible = true;
116+
lbl_seconds.Visible = true;
117+
}
118+
else
119+
{
120+
lbl_apply_profile_delay.Visible = false;
121+
nud_apply_profile_delay.Visible = false;
122+
lbl_seconds.Visible = false;
123+
}
94124
}
95125

96126
private void ProfileSettingsForm_FormClosing(object sender, FormClosingEventArgs e)
@@ -110,6 +140,10 @@ private void ProfileSettingsForm_FormClosing(object sender, FormClosingEventArgs
110140

111141
Profile.WallpaperStyle = ((KeyValuePair<Wallpaper.Style, string>)cmb_wallpaper_display_mode.SelectedItem).Key;
112142

143+
Profile.ApplyProfileCount = (int)nud_apply_profile_count.Value;
144+
145+
Profile.ApplyProfileDelay = (int)nud_apply_profile_delay.Value;
146+
113147
}
114148

115149
private void btn_back_Click(object sender, EventArgs e)
@@ -319,5 +353,27 @@ private void rb_leave_wallpaper_CheckedChanged(object sender, EventArgs e)
319353

320354
}
321355

356+
private void nud_apply_profile_count_ValueChanged(object sender, EventArgs e)
357+
{
358+
_profileSettingChanged = true;
359+
if (nud_apply_profile_count.Value > 1)
360+
{
361+
lbl_apply_profile_delay.Visible = true;
362+
nud_apply_profile_delay.Visible = true;
363+
lbl_seconds.Visible = true;
364+
}
365+
else
366+
{
367+
lbl_apply_profile_delay.Visible = false;
368+
nud_apply_profile_delay.Visible = false;
369+
lbl_seconds.Visible = false;
370+
}
371+
}
372+
373+
private void nud_apply_profile_delay_ValueChanged(object sender, EventArgs e)
374+
{
375+
_profileSettingChanged = true;
376+
}
377+
322378
}
323379
}

DisplayMagician/UIForms/ShortcutForm.Designer.cs

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DisplayMagician/UIForms/ShortcutForm.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,9 @@ private void LoadShortcut()
22392239
// Set up capture permanance as temporary
22402240
_shortcutToEdit.CapturePermanence = ShortcutPermanence.Temporary;
22412241
rb_switch_capture_temp.Checked = true;
2242+
2243+
// Set up the Game Timeouts
2244+
nud_timeout_game.Value = _shortcutToEdit.StartTimeout;
22422245
}
22432246

22442247
if (shortcutTweakChangesName && cb_autosuggest.Checked)

DisplayMagicianShared/ProfileItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ public int ApplyProfileCount
470470
}
471471
}
472472

473-
// The delay in milliseconds between profile attempts. Is only used if there is more than one attempt set in ApplyProfileCount
473+
// The delay in seconds between profile attempts. Is only used if there is more than one attempt set in ApplyProfileCount
474474
[DefaultValue(0)]
475475
public int ApplyProfileDelay
476476
{

DisplayMagicianShared/ProfileRepository.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,10 +1580,11 @@ public static ApplyProfileResult ApplyProfile(ProfileItem profile)
15801580
result = ApplyProfileResult.Successful;
15811581
}
15821582

1583-
if (i > 1 && profile.ApplyProfileDelay > 0 && profile.ApplyProfileDelay < 99999)
1583+
if (i > 1 && profile.ApplyProfileDelay > 0 && profile.ApplyProfileDelay <= 1000)
15841584
{
1585-
// we have more than one profile attempt to go, so delay the requested amount
1586-
Task.Delay(profile.ApplyProfileDelay);
1585+
// we have more than one profile attempt to go, so delay the requested amount, converting seconds to milliseconds.
1586+
// Note - usiong THread.Sleep instead of Task.Delay, as this is not a UI thread and we want to delay this thread.
1587+
Thread.Sleep(profile.ApplyProfileDelay * 1000);
15871588
}
15881589
}
15891590
}

DisplayMagicianShared/Windows/WinLibrary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,7 @@ public bool SetActiveConfig(WINDOWS_DISPLAY_CONFIG displayConfig)
16291629

16301630

16311631
// Wait 0.5 second for the display to settle before trying the CCD settings. This hopefully will make it more reliable setting the primary display as described in issues #78 and #284
1632-
Task.Delay(500);
1632+
Task.Delay(500).Wait();
16331633

16341634

16351635
uint myPathsCount = (uint)displayConfig.DisplayConfigPaths.Length;

0 commit comments

Comments
 (0)