Skip to content

Commit

Permalink
Use Task.Delay instead of a Timer
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Dec 2, 2023
1 parent a2280bb commit 4fabe2f
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions TwitchDownloaderWPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using TwitchDownloaderWPF.Properties;
Expand Down Expand Up @@ -104,7 +104,7 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)

// Flash the window taskbar icon if it is not in the foreground. This is to mitigate a problem where
// it will sometimes start behind other windows, usually (but not always) due to the user's actions.
FlashWindowIfNotForeground(TimeSpan.FromSeconds(4));
await FlashWindowIfNotForeground(TimeSpan.FromSeconds(3));

AutoUpdater.InstalledVersion = currentVersion;
#if !DEBUG
Expand All @@ -117,7 +117,7 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
#endif
}

private void FlashWindowIfNotForeground(TimeSpan flashDuration)
private async Task FlashWindowIfNotForeground(TimeSpan flashDuration)
{
var currentWindow = new WindowInteropHelper(this).Handle;
var foregroundWindow = NativeFunctions.GetForegroundWindow();
Expand All @@ -134,21 +134,17 @@ private void FlashWindowIfNotForeground(TimeSpan flashDuration)
};
_ = NativeFunctions.FlashWindowEx(flashWInfo);

_ = new Timer(static state =>
{
if (state is not IntPtr windowHandle)
return;
await Task.Delay(flashDuration);

var flashWInfo = new NativeFunctions.FlashWInfo
{
StructSize = (uint)Marshal.SizeOf<NativeFunctions.FlashWInfo>(),
WindowHandle = windowHandle,
Flags = NativeFunctions.FlashWInfo.FLASHW_STOP,
FlashCount = 0,
Timeout = 0
};
_ = NativeFunctions.FlashWindowEx(flashWInfo);
}, currentWindow, flashDuration, Timeout.InfiniteTimeSpan);
var stopFlashWInfo = new NativeFunctions.FlashWInfo
{
StructSize = (uint)Marshal.SizeOf<NativeFunctions.FlashWInfo>(),
WindowHandle = currentWindow,
Flags = NativeFunctions.FlashWInfo.FLASHW_STOP,
FlashCount = 0,
Timeout = 0
};
_ = NativeFunctions.FlashWindowEx(stopFlashWInfo);
}

private class FfmpegDownloadProgress : IProgress<ProgressInfo>
Expand Down

0 comments on commit 4fabe2f

Please sign in to comment.