diff --git a/src/modules/FileLocksmith/FileLocksmithLibInterop/interop.cpp b/src/modules/FileLocksmith/FileLocksmithLibInterop/interop.cpp index bc9cd7ee8b..70a3a7fe13 100644 --- a/src/modules/FileLocksmith/FileLocksmithLibInterop/interop.cpp +++ b/src/modules/FileLocksmith/FileLocksmithLibInterop/interop.cpp @@ -77,5 +77,20 @@ namespace FileLocksmith::Interop CloseHandle(process); return true; } + + static System::Boolean WaitForProcess(System::UInt32 pid) + { + HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, pid); + + if (!process) + { + return false; + } + + auto wait_result = WaitForSingleObject(process, INFINITE); + CloseHandle(process); + + return wait_result == WAIT_OBJECT_0; + } }; } diff --git a/src/modules/FileLocksmith/FileLocksmithUI/MainWindow.xaml.cs b/src/modules/FileLocksmith/FileLocksmithUI/MainWindow.xaml.cs index 3d125c0ab3..2503b223e6 100644 --- a/src/modules/FileLocksmith/FileLocksmithUI/MainWindow.xaml.cs +++ b/src/modules/FileLocksmith/FileLocksmithUI/MainWindow.xaml.cs @@ -40,8 +40,7 @@ namespace FileLocksmithUI private void StartFindingProcesses() { - Thread thread = new Thread(FindProcesses); - thread.Start(); + new Thread(FindProcesses).Start(); DisplayProgressRing(); } @@ -64,10 +63,37 @@ namespace FileLocksmithUI stackPanel.Children.Add(entry); // Launch a thread to erase this entry if the process exits + new Thread(() => WatchProcess(item.pid)).Start(); } }); } + private void WatchProcess(uint pid) + { + if (FileLocksmith.Interop.NativeMethods.WaitForProcess(pid)) + { + // This process has exited. + DispatcherQueue.TryEnqueue(() => + { + for (int i = 0; i < stackPanel.Children.Count; i++) + { + var element = stackPanel.Children[i] as ProcessEntry; + if (element == null) + { + continue; + } + + if (element.Pid == pid) + { + stackPanel.Children.RemoveAt(i); + DisplayNoResultsIfEmpty(); + return; + } + } + }); + } + } + private void DisplayNoResultsIfEmpty() { if (stackPanel.Children.Count == 0) diff --git a/src/modules/FileLocksmith/FileLocksmithUI/ProcessEntry.xaml.cs b/src/modules/FileLocksmith/FileLocksmithUI/ProcessEntry.xaml.cs index 694464e8f6..285729e82b 100644 --- a/src/modules/FileLocksmith/FileLocksmithUI/ProcessEntry.xaml.cs +++ b/src/modules/FileLocksmith/FileLocksmithUI/ProcessEntry.xaml.cs @@ -24,11 +24,11 @@ namespace FileLocksmithUI /// public sealed partial class ProcessEntry : UserControl { - private uint pid; + public uint Pid { get; private set; } public ProcessEntry(string process, uint pid, ulong numFiles) { - this.pid = pid; + Pid = pid; InitializeComponent(); processName.Text = process; @@ -49,7 +49,7 @@ namespace FileLocksmithUI private void KillProcessClick(object sender, RoutedEventArgs e) { - if (!FileLocksmith.Interop.NativeMethods.KillProcess(pid)) + if (!FileLocksmith.Interop.NativeMethods.KillProcess(Pid)) { // TODO show something on failure. }