Skip to content

Commit

Permalink
split out kill
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jul 21, 2021
1 parent 1f476e0 commit ab33739
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
20 changes: 20 additions & 0 deletions src/DiffEngineTray/ProcessEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,24 @@ public static bool TryGet(int id, [NotNullWhen(true)] out Process? process)
return false;
}
}

public static void KillAndDispose(this Process process)
{
try
{
process.Kill();
}
catch (InvalidOperationException)
{
// Race condition can cause "No process is associated with this object"
}
catch (Exception exception)
{
ExceptionHandler.Handle($"Failed to kill process. Id:{process.Id} Name: {process.MainModule?.FileName}", exception);
}
finally
{
process.Dispose();
}
}
}
27 changes: 2 additions & 25 deletions src/DiffEngineTray/Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ public TrackedMove AddMove(
ProcessEx.TryGet(processId.Value, out process);
}

var solution = SolutionDirectoryFinder.Find(key);

Log.Information("MoveAdded. Target:{target}, CanKill:{canKill}, ProcessId:{processId}, CommandLine:{commandLine}", targetFile, canKill, processId, $"{exeFile} {arguments}");

var solution = SolutionDirectoryFinder.Find(key);
Expand Down Expand Up @@ -220,7 +218,6 @@ static void InnerDiscard(TrackedMove move)
FileEx.SafeDelete(move.Temp);
}


static void KillProcesses(TrackedMove move)
{
if (!move.CanKill)
Expand All @@ -235,27 +232,7 @@ static void KillProcesses(TrackedMove move)
return;
}

KillProcess(move, move.Process);
}

static void KillProcess(TrackedMove move, Process process)
{
try
{
process.Kill();
}
catch (InvalidOperationException)
{
// Race condition can cause "No process is associated with this object"
}
catch (Exception exception)
{
ExceptionHandler.Handle($"Failed to kill process. Command: {move.Exe} {move.Arguments}", exception);
}
finally
{
process.Dispose();
}
move.Process.KillAndDispose();
}

public void Clear()
Expand Down Expand Up @@ -298,7 +275,7 @@ public void AcceptAll()
AcceptAllMoves();
}

public void AcceptAllDeletes()
void AcceptAllDeletes()
{
foreach (var delete in deletes.Values)
{
Expand Down

0 comments on commit ab33739

Please sign in to comment.