[PT Run] Async the OnRename to unblock thread (#37987)

* Rename name to async and try catch to handle exception.

* Fix ut

---------

Co-authored-by: Yu Leng (from Dev Box) <yuleng@microsoft.com>
This commit is contained in:
moooyo
2025-03-18 15:51:34 +08:00
committed by GitHub
parent a298f67b81
commit abd6314b2e
2 changed files with 37 additions and 8 deletions

View File

@@ -33,6 +33,8 @@ namespace Microsoft.Plugin.Program.Storage
private static ConcurrentQueue<string> commonEventHandlingQueue = new ConcurrentQueue<string>();
public static readonly int OnRenamedEventWaitTime = 1000;
public Win32ProgramRepository(IList<IFileSystemWatcherWrapper> fileSystemWatcherHelpers, ProgramPluginSettings settings, string[] pathsToWatch)
{
_fileSystemWatcherHelpers = fileSystemWatcherHelpers;
@@ -91,7 +93,7 @@ namespace Microsoft.Plugin.Program.Storage
}
}
private void OnAppRenamed(object sender, RenamedEventArgs e)
private async Task DoOnAppRenamedAsync(object sender, RenamedEventArgs e)
{
string oldPath = e.OldFullPath;
string newPath = e.FullPath;
@@ -100,7 +102,7 @@ namespace Microsoft.Plugin.Program.Storage
// the msi installer creates a shortcut, which is detected by the PT Run and ends up in calling this OnAppRenamed method
// the thread needs to be halted for a short time to avoid locking the new shortcut file as we read it, otherwise the lock causes
// in the issue scenario that a warning is popping up during the msi install process.
System.Threading.Thread.Sleep(1000);
await Task.Delay(OnRenamedEventWaitTime).ConfigureAwait(false);
string extension = Path.GetExtension(newPath);
Win32Program.ApplicationType oldAppType = Win32Program.GetAppTypeFromPath(oldPath);
@@ -124,7 +126,7 @@ namespace Microsoft.Plugin.Program.Storage
}
catch (Exception ex)
{
Log.Exception($"OnAppRenamed-{extension} Program|{e.OldName}|Unable to create program from {oldPath}", ex, GetType());
Log.Exception($"DoOnAppRenamedAsync-{extension} Program|{e.OldName}|Unable to create program from {oldPath}", ex, GetType());
}
// To remove the old app which has been renamed and to add the new application.
@@ -146,6 +148,21 @@ namespace Microsoft.Plugin.Program.Storage
}
}
private void OnAppRenamed(object sender, RenamedEventArgs e)
{
Task.Run(async () =>
{
try
{
await DoOnAppRenamedAsync(sender, e).ConfigureAwait(false);
}
catch (Exception e)
{
Log.Exception($"OnAppRenamed throw exception.", e, e.GetType());
}
}).ConfigureAwait(false);
}
private void OnAppDeleted(object sender, FileSystemEventArgs e)
{
string path = e.FullPath;