Skip to content

Commit

Permalink
Make application more resilient when device if offline.
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimian committed Aug 8, 2021
1 parent 059547c commit b52cb00
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
29 changes: 18 additions & 11 deletions source/TotalMixVC.Communicator/VolumeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,15 @@ public float VolumeMax
/// <returns>The task object representing the asynchronous operation.</returns>
public async Task GetDeviceVolumeAsync()
{
while (Volume == -1.0f)
{
// Send an initial invalid value (-1.0) so that TotalMix can send us the current
// volume.
await SendCurrentVolumeAsync().ConfigureAwait(false);
// Send an initial invalid value (-1.0) so that TotalMix can send us the current
// volume.
await SendCurrentVolumeAsync().ConfigureAwait(false);

// Wait up until one second for the current volume to updated by the listener.
// If no update is received, the initial value will be resent.
for (uint iterations = 0; Volume == -1.0f && iterations < 40; iterations++)
{
await Task.Delay(25).ConfigureAwait(false);
}
// Wait up until one second for the current volume to updated by the listener.
// If no update is received, the initial value will be resent.
for (uint iterations = 0; Volume == -1.0f && iterations < 40; iterations++)
{
await Task.Delay(25).ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -204,6 +201,11 @@ public async Task<bool> ReceiveVolumeAsync(int timeout = 5000)
/// </returns>
public async Task<bool> IncreaseVolumeAsync(bool fine = false)
{
if (Volume == -1.0f)
{
return false;
}

await _volumeMutex.WaitAsync().ConfigureAwait(false);
try
{
Expand Down Expand Up @@ -243,6 +245,11 @@ public async Task<bool> IncreaseVolumeAsync(bool fine = false)
/// </returns>
public async Task<bool> DecreaseVolumeAsync(bool fine = false)
{
if (Volume == -1.0f)
{
return false;
}

await _volumeMutex.WaitAsync().ConfigureAwait(false);
try
{
Expand Down
15 changes: 11 additions & 4 deletions source/TotalMixVC.GUI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public partial class App : Application

private Task _volumeReceiveTask;

private Task _volumeInitializeTask;

private TaskbarIcon _trayIcon;

private TextBlock _trayToolTipStatusTextBlock;
Expand Down Expand Up @@ -118,10 +120,14 @@ private void App_Startup(object sender, StartupEventArgs e)
}
});

// Ensure we obtain the current device volume before registering hotkeys.
Task
.Run(async () => await volumeManager.GetDeviceVolumeAsync().ConfigureAwait(false))
.Wait();
// Obtain the current device volume.
_volumeInitializeTask = Task.Run(async () =>
{
while (_running && volumeManager.Volume == -1.0f)
{
await volumeManager.GetDeviceVolumeAsync().ConfigureAwait(false);
}
});

// Register all the hotkeys for changing the volume. Note that doing this does not
// work inside a task so this must be performed in the main method scope.
Expand Down Expand Up @@ -164,6 +170,7 @@ private void App_Exit(object sender, ExitEventArgs e)
{
_running = false;
_volumeReceiveTask.Wait();
_volumeInitializeTask.Wait();
_trayIcon.Dispose();
}
}
Expand Down

0 comments on commit b52cb00

Please sign in to comment.