Skip to content

Commit 999dd6a

Browse files
Fix RealtimeScanProgressIndicator to properly manage VS status bar progress
Issues fixed: 1. Silent catch blocks: log errors via CxAssistErrorHandler instead of swallowing - PushScanAsync: log progress bar initialization errors - PopScanAsync: log progress cleanup errors - ProgressTimer.Elapsed: log timer execution errors - Prevents bugs from being hidden forever (enables debugging and diagnostics) 2. VS Progress contract violation: initialize progress before timer starts - Call statusBar.Progress(ref _progressCookie=0, fInProgress=1) to let VS allocate cookie - Timer updates with same cookie to fill the bar (0→100%) - PopScanAsync calls with fInProgress=0 to clear, then resets cookie to 0 - Prevents progress bar from sticking at 100% after scan completes 3. Add clarifying comments on VS Progress API contract requirements Addresses code review feedback from @cx-rakesh-kadu Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent ff515da commit 999dd6a

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

ast-visual-studio-extension/CxExtension/CxAssist/Realtime/Utils/RealtimeScanProgressIndicator.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ internal static async Task PushScanAsync(string scannerName, string sourceFilePa
4747
_currentProgress = 0;
4848
StartProgressBar(label);
4949
}
50-
catch
50+
catch (Exception ex)
5151
{
52+
CxAssistErrorHandler.LogAndSwallow(ex, "RealtimeScanProgressIndicator.PushScanAsync");
5253
TrySetTextFallback($"Checkmarx is Scanning File : {fileName}");
5354
}
5455
}
@@ -85,8 +86,9 @@ internal static async Task PopScanAsync()
8586
TrySetTextFallback(label);
8687
}
8788
}
88-
catch
89+
catch (Exception ex)
8990
{
91+
CxAssistErrorHandler.LogAndSwallow(ex, "RealtimeScanProgressIndicator.PopScanAsync");
9092
if (_depth == 0)
9193
{
9294
StopProgressBar();
@@ -110,13 +112,31 @@ private static void ResetProgress()
110112

111113
/// <summary>
112114
/// Starts progress bar that fills 0-100% once during scan.
115+
/// VS Progress contract: initialize with cookie=0 and fInProgress=1 (VS allocates cookie),
116+
/// then update with the same cookie, then call with fInProgress=0 to clear.
113117
/// Updates every 200ms with +10% increment = ~2 second fill time.
114118
/// </summary>
115119
private static void StartProgressBar(string label)
116120
{
117121
StopProgressBar();
118122

119123
_currentProgress = 0;
124+
125+
// Initialize progress bar (VS allocates _progressCookie when passed as 0)
126+
var statusBar = Package.GetGlobalService(typeof(SVsStatusbar)) as IVsStatusbar;
127+
if (statusBar != null)
128+
{
129+
try
130+
{
131+
statusBar.Progress(ref _progressCookie, 1, label, 0, 100);
132+
}
133+
catch (Exception ex)
134+
{
135+
CxAssistErrorHandler.LogAndSwallow(ex, "RealtimeScanProgressIndicator.StartProgressBar");
136+
return;
137+
}
138+
}
139+
120140
_progressTimer = new System.Timers.Timer(200);
121141
_progressTimer.Elapsed += (sender, e) =>
122142
{
@@ -128,10 +148,10 @@ private static void StartProgressBar(string label)
128148
if (_currentProgress > 100)
129149
_currentProgress = 100;
130150

131-
var statusBar = Package.GetGlobalService(typeof(SVsStatusbar)) as IVsStatusbar;
132-
if (statusBar != null)
151+
var sb = Package.GetGlobalService(typeof(SVsStatusbar)) as IVsStatusbar;
152+
if (sb != null)
133153
{
134-
statusBar.Progress(ref _progressCookie, 1, label, _currentProgress, 100);
154+
sb.Progress(ref _progressCookie, 1, label, _currentProgress, 100);
135155
}
136156

137157
// Stop timer once progress reaches 100%
@@ -141,9 +161,9 @@ private static void StartProgressBar(string label)
141161
}
142162
}
143163
}
144-
catch
164+
catch (Exception ex)
145165
{
146-
// Ignore timer errors
166+
CxAssistErrorHandler.LogAndSwallow(ex, "RealtimeScanProgressIndicator.ProgressTimer");
147167
}
148168
};
149169
_progressTimer.AutoReset = true;

0 commit comments

Comments
 (0)