Remove entries for killed processes

This commit is contained in:
Ivan Stošić 2022-09-26 18:05:52 +02:00
parent 17d4fac5c0
commit a7fd4593b8
5 changed files with 49 additions and 12 deletions

View File

@ -46,6 +46,11 @@ namespace winrt::FileLocksmithGUI::implementation
{
ProcessEntry entry(process.name, process.pid, process.num_files);
stackPanel().Children().Append(entry);
// Launch a thread to erase this entry if the process exits
std::thread([&, pid = process.pid] {
watch_process(pid);
}).detach();
}
if (process_info.empty())
@ -115,4 +120,40 @@ namespace winrt::FileLocksmithGUI::implementation
text_block.VerticalAlignment(VerticalAlignment::Center);
stackPanel().Children().Append(text_block);
}
void MainWindow::watch_process(DWORD pid)
{
HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, pid);
if (!process)
{
return;
}
auto wait_result = WaitForSingleObject(process, INFINITE);
CloseHandle(process);
if (wait_result == WAIT_OBJECT_0)
{
// Find entry with this PID and erase it
DispatcherQueue().TryEnqueue([&, pid] {
for (uint32_t i = 0; i < stackPanel().Children().Size(); i++)
{
auto element = stackPanel().Children().GetAt(i);
auto process_entry = element.try_as<ProcessEntry>();
if (!process_entry)
{
continue;
}
if (process_entry.Pid() == pid)
{
stackPanel().Children().RemoveAt(i);
return;
}
}
});
}
}
}

View File

@ -13,6 +13,7 @@ namespace winrt::FileLocksmithGUI::implementation
void find_processes();
void place_and_resize();
void display_text_info(std::wstring text);
void watch_process(DWORD pid);
};
}

View File

@ -4,5 +4,6 @@ namespace FileLocksmithGUI
runtimeclass ProcessEntry : Microsoft.UI.Xaml.Controls.UserControl
{
ProcessEntry(String process, UInt32 pid, UInt64 num_files);
UInt32 Pid();
}
}

View File

@ -36,17 +36,10 @@ namespace winrt::FileLocksmithGUI::implementation
}
CloseHandle(process);
auto siblings = Parent().as<Controls::StackPanel>().Children();
if (uint32_t index; siblings.IndexOf(*this, index))
{
siblings.RemoveAt(index);
}
if (siblings.Size() == 0)
{
auto main_window = Parent().as<Controls::StackPanel>().Parent().as<MainWindow>();
main_window.DisplayNoResultsInfo();
}
}
DWORD ProcessEntry::Pid()
{
return m_pid;
}
}

View File

@ -11,6 +11,7 @@ namespace winrt::FileLocksmithGUI::implementation
{
ProcessEntry(const winrt::hstring& process, DWORD pid, uint64_t num_files);
void killProcessClick(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e);
DWORD Pid();
private:
DWORD m_pid;
};