Remove entries for terminated processes

This commit is contained in:
Ivan Stošić 2022-10-05 15:20:31 +02:00
parent 5d4b9d68e2
commit c7f6eda05a
3 changed files with 46 additions and 5 deletions

View File

@ -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;
}
};
}

View File

@ -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)

View File

@ -24,11 +24,11 @@ namespace FileLocksmithUI
/// </summary>
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.
}