Skip to content

Commit

Permalink
Use async when opening folders
Browse files Browse the repository at this point in the history
Prevent UI thread stuck when explorer decides to be slow
  • Loading branch information
bagusnl committed Nov 3, 2024
1 parent 11fe45e commit eec32dc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 55 deletions.
66 changes: 42 additions & 24 deletions CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2000,38 +2000,56 @@ private void OpenScreenshot_Invoked(KeyboardAccelerator sender, KeyboardAccelera
}.Start();
}

private void OpenGameFolder_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
private async void OpenGameFolder_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
if (!IsGameInstalled()) return;
try
{
if (!IsGameInstalled()) return;

string GameFolder = NormalizePath(GameDirPath);
LogWriteLine($"Opening Game Folder:\r\n\t{GameFolder}");
new Process()
string GameFolder = NormalizePath(GameDirPath);
LogWriteLine($"Opening Game Folder:\r\n\t{GameFolder}");
await Task.Run(() =>
new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = "explorer.exe",
Arguments = GameFolder
}
}.Start());
}
catch (Exception ex)
{
StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = "explorer.exe",
Arguments = GameFolder
}
}.Start();
LogWriteLine($"Failed when trying to open game folder!\r\n{ex}", LogType.Error, true);
ErrorSender.SendException(ex);
}
}

private void OpenGameCacheFolder_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
private async void OpenGameCacheFolder_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
if (!IsGameInstalled()) return;
try
{
if (!IsGameInstalled()) return;

string GameFolder = CurrentGameProperty._GameVersion.GameDirAppDataPath;
LogWriteLine($"Opening Game Folder:\r\n\t{GameFolder}");
new Process()
string GameFolder = CurrentGameProperty._GameVersion.GameDirAppDataPath;
LogWriteLine($"Opening Game Folder:\r\n\t{GameFolder}");
await Task.Run(() =>
new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = "explorer.exe",
Arguments = GameFolder
}
}.Start());
}
catch (Exception ex)
{
StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = "explorer.exe",
Arguments = GameFolder
}
}.Start();
LogWriteLine($"Failed when trying to open game cache folder!\r\n{ex}", LogType.Error, true);
ErrorSender.SendException(ex);
}
}

private void ForceCloseGame_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
Expand Down
90 changes: 59 additions & 31 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2199,37 +2199,56 @@ private async void ReadOutputLog()
#endregion

#region Open Button Method
private void OpenGameFolderButton_Click(object sender, RoutedEventArgs e)
private async void OpenGameFolderButton_Click(object sender, RoutedEventArgs e)
{
string GameFolder = NormalizePath(GameDirPath);
LogWriteLine($"Opening Game Folder:\r\n\t{GameFolder}");
new Process()
try
{
StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = "explorer.exe",
Arguments = GameFolder
}
}.Start();
string gameFolder = NormalizePath(GameDirPath);
LogWriteLine($"Opening Game Folder:\r\n\t{gameFolder}");

await Task.Run(() =>
new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = "explorer.exe",
Arguments = gameFolder
}
}.Start());
}
catch (Exception ex)
{
LogWriteLine($"Failed when trying to open game folder!\r\n{ex}", LogType.Error, true);
ErrorSender.SendException(ex);
}
}

private void OpenCacheFolderButton_Click(object sender, RoutedEventArgs e)
private async void OpenCacheFolderButton_Click(object sender, RoutedEventArgs e)
{
string GameFolder = CurrentGameProperty._GameVersion.GameDirAppDataPath;
LogWriteLine($"Opening Game Folder:\r\n\t{GameFolder}");
new Process()
string cacheFolder = CurrentGameProperty._GameVersion.GameDirAppDataPath;
LogWriteLine($"Opening Game Folder:\r\n\t{cacheFolder}");
try
{
StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = "explorer.exe",
Arguments = GameFolder
}
}.Start();
await Task.Run(() =>
new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = "explorer.exe",
Arguments = cacheFolder
}
}.Start());
}
catch (Exception ex)
{
LogWriteLine($"Failed when trying to open game cache folder!\r\n{ex}", LogType.Error, true);
ErrorSender.SendException(ex);
}
}

private void OpenScreenshotFolderButton_Click(object sender, RoutedEventArgs e)
private async void OpenScreenshotFolderButton_Click(object sender, RoutedEventArgs e)
{
string ScreenshotFolder = Path.Combine(NormalizePath(GameDirPath), CurrentGameProperty._GameVersion.GamePreset.GameType switch
{
Expand All @@ -2242,15 +2261,24 @@ private void OpenScreenshotFolderButton_Click(object sender, RoutedEventArgs e)
if (!Directory.Exists(ScreenshotFolder))
Directory.CreateDirectory(ScreenshotFolder);

new Process()
try
{
StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = "explorer.exe",
Arguments = ScreenshotFolder
}
}.Start();
await Task.Run(() =>
new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = "explorer.exe",
Arguments = ScreenshotFolder
}
}.Start());
}
catch (Exception ex)
{
LogWriteLine($"Failed when trying to open game screenshot folder!\r\n{ex}", LogType.Error, true);
ErrorSender.SendException(ex);
}
}

private async void CleanupFilesButton_Click(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit eec32dc

Please sign in to comment.