[FileLocksmith]Fix crash while opening process handlers (#21602)

* [FileLocksmith]Fix crash while opening processes

* Remove binding on IsExpanded. It's not working
This commit is contained in:
Jaime Bernardo
2022-11-02 11:38:04 +00:00
committed by GitHub
parent b9ccb9f049
commit 7b7742719c
3 changed files with 30 additions and 16 deletions

View File

@@ -11,7 +11,6 @@ namespace FileLocksmith::Interop
System::String^ name; System::String^ name;
System::UInt32 pid; System::UInt32 pid;
array<System::String^>^ files; array<System::String^>^ files;
System::Boolean isExpanded; // For helping in the UI
}; };
System::String^ from_wstring_view(std::wstring_view str) System::String^ from_wstring_view(std::wstring_view str)
@@ -77,7 +76,6 @@ namespace FileLocksmith::Interop
{ {
item->files[j] = from_wstring_view(result_cpp[i].files[j]); item->files[j] = from_wstring_view(result_cpp[i].files[j]);
} }
item->isExpanded = false;
result[i] = item; result[i] = item;
} }

View File

@@ -118,6 +118,8 @@ namespace PowerToys.FileLocksmithUI.ViewModels
} }
private async void WatchProcess(ProcessResult process, CancellationToken token) private async void WatchProcess(ProcessResult process, CancellationToken token)
{
try
{ {
Process handle = Process.GetProcessById((int)process.pid); Process handle = Process.GetProcessById((int)process.pid);
try try
@@ -134,18 +136,32 @@ namespace PowerToys.FileLocksmithUI.ViewModels
Processes.Remove(process); Processes.Remove(process);
} }
} }
catch (Exception ex)
{
Logger.LogError($"Couldn't add a waiter to wait for a process to exit. PID = {process.pid} and Name = {process.name}.", ex);
Processes.Remove(process); // If we couldn't get an handle to the process or it has exited in the meanwhile, don't show it.
}
}
[RelayCommand] [RelayCommand]
public void EndTask(ProcessResult selectedProcess) public void EndTask(ProcessResult selectedProcess)
{
try
{ {
Process handle = Process.GetProcessById((int)selectedProcess.pid); Process handle = Process.GetProcessById((int)selectedProcess.pid);
try try
{ {
handle.Kill(); handle.Kill();
} }
catch (Exception) catch (Exception ex)
{ {
Logger.LogError($"Couldn't kill process {selectedProcess.name} with PID {selectedProcess.pid}."); Logger.LogError($"Couldn't kill process {selectedProcess.name} with PID {selectedProcess.pid}.", ex);
}
}
catch (Exception ex)
{
Logger.LogError($"Couldn't get an handle to kill process {selectedProcess.name} with PID {selectedProcess.pid}. Likely has been killed already.", ex);
Processes.Remove(selectedProcess); // If we couldn't get an handle to the process, remove it from the list, since it's likely been killed already.
} }
} }

View File

@@ -103,7 +103,7 @@
SelectionMode="None"> SelectionMode="None">
<ListView.ItemTemplate> <ListView.ItemTemplate>
<DataTemplate x:DataType="interop:ProcessResult"> <DataTemplate x:DataType="interop:ProcessResult">
<labs:SettingsExpander IsExpanded="{Binding isExpanded}" Margin="0,3,0,0"> <labs:SettingsExpander Margin="0,3,0,0">
<labs:SettingsExpander.Header> <labs:SettingsExpander.Header>
<!-- We can't use the HeaderIcon because it only support a BitmapIcon, which only supports UriSource - not a direct BitmapImage --> <!-- We can't use the HeaderIcon because it only support a BitmapIcon, which only supports UriSource - not a direct BitmapImage -->
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">