-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathClaudeCodeControl.Cleanup.cs
More file actions
229 lines (204 loc) · 8.04 KB
/
ClaudeCodeControl.Cleanup.cs
File metadata and controls
229 lines (204 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* *******************************************************************************************************************
* Application: ClaudeCodeExtension
*
* Autor: Daniel Carvalho Liedke
*
* Copyright © Daniel Carvalho Liedke 2026
* Usage and reproduction in any manner whatsoever without the written permission of Daniel Carvalho Liedke is strictly forbidden.
*
* Purpose: Resource cleanup and temporary file management
*
* *******************************************************************************************************************/
using System;
using System.IO;
using System.Diagnostics;
using System.Windows;
using Microsoft.VisualStudio.Shell;
namespace ClaudeCodeVS
{
public partial class ClaudeCodeControl
{
#region Temporary Directory Fields
/// <summary>
/// Session-specific temporary directory for storing pasted images
/// </summary>
private string tempImageDirectory;
#endregion
#region Temporary Directory Initialization
/// <summary>
/// Initializes the temporary directory for storing pasted images
/// Cleans up any existing ClaudeCodeVS temp directories first
/// </summary>
private void InitializeTempDirectory()
{
try
{
// Clear any existing ClaudeCodeVS temp directories
CleanupClaudeCodeVSTempDirectories();
// Create a session-level temp directory for storing pasted images before sending
tempImageDirectory = Path.Combine(Path.GetTempPath(), "ClaudeCodeVS_Session", Guid.NewGuid().ToString());
Directory.CreateDirectory(tempImageDirectory);
}
catch (Exception ex)
{
Debug.WriteLine($"Error creating temp directory: {ex.Message}");
// Fallback to a simpler path
tempImageDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempImageDirectory);
}
}
/// <summary>
/// Cleans up all ClaudeCodeVS temporary directories from previous sessions
/// </summary>
private void CleanupClaudeCodeVSTempDirectories()
{
try
{
string tempPath = Path.GetTempPath();
// Clean up old ClaudeCodeVS directories
string claudeCodeVSPath = Path.Combine(tempPath, "ClaudeCodeVS");
if (Directory.Exists(claudeCodeVSPath))
{
Directory.Delete(claudeCodeVSPath, true);
}
// Clean up session directories
string sessionPath = Path.Combine(tempPath, "ClaudeCodeVS_Session");
if (Directory.Exists(sessionPath))
{
Directory.Delete(sessionPath, true);
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error cleaning up ClaudeCodeVS temp directories: {ex.Message}");
// Continue even if cleanup fails
}
}
#endregion
#region Unload and Cleanup
/// <summary>
/// Handles control unload event - keeps terminal alive for tab switches
/// </summary>
private void ClaudeCodeControl_Unloaded(object sender, RoutedEventArgs e)
{
// Don't save during unload - settings should only be saved when user makes changes
// NOTE: Don't cleanup terminal here - Unloaded fires during tab switches
// Terminal cleanup only happens in Dispose() when VS is actually closing
}
/// <summary>
/// Cleans up all resources including processes and temporary files
/// </summary>
private void CleanupResources()
{
ThreadHelper.ThrowIfNotOnUIThread();
try
{
// Cleanup diff tracking
CleanupDiffTracking();
// Unsubscribe from theme change events
CleanupThemeEvents();
// Kill child processes (like claude.exe) before killing the main cmd process
if (cmdProcess != null && !cmdProcess.HasExited)
{
try
{
KillProcessAndChildren(cmdProcess.Id);
}
catch (Exception ex)
{
Debug.WriteLine($"Error killing child processes: {ex.Message}");
}
}
// Kill and dispose terminal process
if (cmdProcess != null && !cmdProcess.HasExited)
{
try
{
cmdProcess.Kill();
cmdProcess.Dispose();
}
catch (Exception ex)
{
Debug.WriteLine($"Error disposing cmd process: {ex.Message}");
}
cmdProcess = null;
}
// Clean up temporary directory
if (Directory.Exists(tempImageDirectory))
{
try
{
Directory.Delete(tempImageDirectory, true);
}
catch (Exception ex)
{
Debug.WriteLine($"Error cleaning temp directory: {ex.Message}");
// Try to at least delete files if directory deletion fails
try
{
foreach (string file in Directory.GetFiles(tempImageDirectory))
{
File.Delete(file);
}
}
catch { }
}
}
// Clear attached images list
attachedImagePaths?.Clear();
}
catch (Exception ex)
{
Debug.WriteLine($"Error during cleanup: {ex.Message}");
}
}
/// <summary>
/// Kills a process and all its child processes
/// </summary>
/// <param name="processId">The process ID to kill</param>
private void KillProcessAndChildren(int processId)
{
try
{
// Use WMI to find and kill child processes
using (var searcher = new System.Management.ManagementObjectSearcher(
$"SELECT ProcessId FROM Win32_Process WHERE ParentProcessId={processId}"))
{
foreach (var obj in searcher.Get())
{
try
{
int childProcessId = Convert.ToInt32(obj["ProcessId"]);
// Recursively kill children of this child
KillProcessAndChildren(childProcessId);
// Kill the child process
var childProcess = Process.GetProcessById(childProcessId);
if (!childProcess.HasExited)
{
childProcess.Kill();
childProcess.Dispose();
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error killing child process: {ex.Message}");
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error in KillProcessAndChildren: {ex.Message}");
}
}
/// <summary>
/// Implements IDisposable - disposes of all managed resources
/// </summary>
public void Dispose()
{
ThreadHelper.ThrowIfNotOnUIThread();
CleanupResources();
}
#endregion
}
}