Skip to content

Commit 7b77427

Browse files
[FileLocksmith]Fix crash while opening process handlers (#21602)
* [FileLocksmith]Fix crash while opening processes * Remove binding on IsExpanded. It's not working
1 parent b9ccb9f commit 7b77427

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/modules/FileLocksmith/FileLocksmithLibInterop/interop.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace FileLocksmith::Interop
1111
System::String^ name;
1212
System::UInt32 pid;
1313
array<System::String^>^ files;
14-
System::Boolean isExpanded; // For helping in the UI
1514
};
1615

1716
System::String^ from_wstring_view(std::wstring_view str)
@@ -77,7 +76,6 @@ namespace FileLocksmith::Interop
7776
{
7877
item->files[j] = from_wstring_view(result_cpp[i].files[j]);
7978
}
80-
item->isExpanded = false;
8179

8280
result[i] = item;
8381
}

src/modules/FileLocksmith/FileLocksmithUI/ViewModels/MainViewModel.cs

+29-13
Original file line numberDiff line numberDiff line change
@@ -119,33 +119,49 @@ await Task.Run(() =>
119119

120120
private async void WatchProcess(ProcessResult process, CancellationToken token)
121121
{
122-
Process handle = Process.GetProcessById((int)process.pid);
123122
try
124123
{
125-
await handle.WaitForExitAsync(token);
126-
}
127-
catch (TaskCanceledException)
128-
{
129-
// Nothing to do, normal operation
130-
}
124+
Process handle = Process.GetProcessById((int)process.pid);
125+
try
126+
{
127+
await handle.WaitForExitAsync(token);
128+
}
129+
catch (TaskCanceledException)
130+
{
131+
// Nothing to do, normal operation
132+
}
131133

132-
if (handle.HasExited)
134+
if (handle.HasExited)
135+
{
136+
Processes.Remove(process);
137+
}
138+
}
139+
catch (Exception ex)
133140
{
134-
Processes.Remove(process);
141+
Logger.LogError($"Couldn't add a waiter to wait for a process to exit. PID = {process.pid} and Name = {process.name}.", ex);
142+
Processes.Remove(process); // If we couldn't get an handle to the process or it has exited in the meanwhile, don't show it.
135143
}
136144
}
137145

138146
[RelayCommand]
139147
public void EndTask(ProcessResult selectedProcess)
140148
{
141-
Process handle = Process.GetProcessById((int)selectedProcess.pid);
142149
try
143150
{
144-
handle.Kill();
151+
Process handle = Process.GetProcessById((int)selectedProcess.pid);
152+
try
153+
{
154+
handle.Kill();
155+
}
156+
catch (Exception ex)
157+
{
158+
Logger.LogError($"Couldn't kill process {selectedProcess.name} with PID {selectedProcess.pid}.", ex);
159+
}
145160
}
146-
catch (Exception)
161+
catch (Exception ex)
147162
{
148-
Logger.LogError($"Couldn't kill process {selectedProcess.name} with PID {selectedProcess.pid}.");
163+
Logger.LogError($"Couldn't get an handle to kill process {selectedProcess.name} with PID {selectedProcess.pid}. Likely has been killed already.", ex);
164+
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.
149165
}
150166
}
151167

src/modules/FileLocksmith/FileLocksmithUI/Views/MainPage.xaml

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

0 commit comments

Comments
 (0)