Skip to content
This repository was archived by the owner on Aug 9, 2025. It is now read-only.

Commit 0501940

Browse files
committed
Possible fix for #211 - total freeze of Visual Studio
1 parent 9efa905 commit 0501940

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

CPPCheckPlugin/AtomicBool.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace VSPackage.CPPCheckPlugin
4+
{
5+
public class AtomicBool
6+
{
7+
public AtomicBool()
8+
{
9+
}
10+
11+
[MethodImpl(MethodImplOptions.Synchronized)]
12+
public AtomicBool(bool value)
13+
{
14+
_value = value;
15+
}
16+
17+
[MethodImpl(MethodImplOptions.Synchronized)]
18+
public static implicit operator bool(AtomicBool a)
19+
{
20+
return a._value;
21+
}
22+
23+
public bool Value
24+
{
25+
[MethodImpl(MethodImplOptions.Synchronized)]
26+
get => _value;
27+
[MethodImpl(MethodImplOptions.Synchronized)]
28+
set { _value = value; }
29+
}
30+
31+
private bool _value = false;
32+
}
33+
}

CPPCheckPlugin/CPPCheckPlugin.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
</ItemGroup>
182182
<ItemGroup>
183183
<Compile Include="AnalyzerCppcheck.cs" />
184+
<Compile Include="AtomicBool.cs" />
184185
<Compile Include="ChecksPanel.cs" />
185186
<Compile Include="CppcheckMessagesList.xaml.cs">
186187
<DependentUpon>CppcheckMessagesList.xaml</DependentUpon>

CPPCheckPlugin/CPPCheckPluginPackage.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
193193

194194
{
195195
CommandID selectionsMenuCommandID = new CommandID(GuidList.guidCPPCheckPluginCmdSet, (int)PkgCmdIDList.cmdidCheckMultiItemCppcheck);
196-
menuCheckSelections = new MenuCommand(onCheckSelectionsRequested, selectionsMenuCommandID);
196+
menuCheckSelections = new MenuCommand(onCheckSelectedProjects, selectionsMenuCommandID);
197197
mcs.AddCommand(menuCheckSelections);
198198
}
199199

@@ -211,7 +211,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
211211

212212
{
213213
CommandID selectionsMenuCommandID = new CommandID(GuidList.guidCPPCheckPluginMultiItemProjectCmdSet, (int)PkgCmdIDList.cmdidCheckMultiItemCppcheck1);
214-
checkMultiSelections = new MenuCommand(onCheckSelectionsRequested, selectionsMenuCommandID);
214+
checkMultiSelections = new MenuCommand(onCheckSelectedProjects, selectionsMenuCommandID);
215215
mcs.AddCommand(checkMultiSelections);
216216
}
217217

@@ -247,8 +247,7 @@ private void onCheckCurrentProjectRequested(object sender, EventArgs e)
247247
{
248248
JoinableTaskFactory.Run(async () =>
249249
{
250-
await JoinableTaskFactory.SwitchToMainThreadAsync();
251-
_ = checkFirstActiveProjectAsync();
250+
await checkFirstActiveProjectAsync();
252251
});
253252
}
254253

@@ -269,7 +268,7 @@ private void onCheckAllProjectsRequested(object sender, EventArgs e)
269268
});
270269
}
271270

272-
private void onCheckSelectionsRequested(object sender, EventArgs e)
271+
private void onCheckSelectedProjects(object sender, EventArgs e)
273272
{
274273
JoinableTaskFactory.Run(async () =>
275274
{
@@ -461,6 +460,7 @@ private async Task<SourceFilesWithConfiguration> getAllSupportedFilesFromProject
461460
private async Task checkFirstActiveProjectAsync()
462461
{
463462
await JoinableTaskFactory.SwitchToMainThreadAsync();
463+
464464

465465
var activeProjects = await findSelectedCppProjectsAsync();
466466
Assumes.NotNull(activeProjects);
@@ -507,7 +507,10 @@ private async Task checkProjectsAsync(List<Project> projects)
507507

508508
foreach (ProjectItem projectItem in project.ProjectItems)
509509
{
510+
Stopwatch sw = Stopwatch.StartNew();
510511
await scanProjectItemForSourceFilesAsync(projectItem, sourceFiles, config, project);
512+
sw.Stop();
513+
await AddTextToOutputWindowAsync("scanProjectItemForSourceFilesAsync for " + projectItem.Name + " took " + sw.ElapsedMilliseconds + " ms\n");
511514
}
512515

513516
// Although we're using the same base configuration, it's possible for each file to override that.
@@ -783,17 +786,17 @@ private async void scanProgressUpdated(int filesScanned)
783786
statusBar.Clear();
784787
}
785788
}
786-
catch (Exception) { }
789+
catch (Exception) {}
787790
}
788791

789792
private async void updateStatusBarProgress(bool inProgress, string label, int currentPercentage)
790793
{
791-
await JoinableTaskFactory.SwitchToMainThreadAsync();
792794
try
793795
{
796+
await JoinableTaskFactory.SwitchToMainThreadAsync();
794797
_dte.StatusBar.Progress(inProgress, label, currentPercentage, 100);
795798
}
796-
catch (Exception) { }
799+
catch (Exception) {}
797800
}
798801

799802
private async void checkProgressUpdated(object sender, ICodeAnalyzer.ProgressEvenArgs e)

CPPCheckPlugin/ICodeAnalyzer.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.Text.RegularExpressions;
66
using System.IO;
7+
using System.Threading;
78

89
namespace VSPackage.CPPCheckPlugin
910
{
@@ -160,8 +161,11 @@ public void abortThreadIfAny()
160161
{
161162
try
162163
{
163-
_terminateThread = true;
164-
_thread.Join();
164+
_terminateThread.Value = true;
165+
if (!_thread.Join(TimeSpan.FromSeconds(5)))
166+
{
167+
_thread.Abort();
168+
}
165169
}
166170
catch (Exception ex)
167171
{
@@ -173,7 +177,7 @@ public void abortThreadIfAny()
173177

174178
private void analyzerThreadFunc(string analyzerExePath)
175179
{
176-
_terminateThread = false;
180+
_terminateThread.Value = false;
177181
foreach (var arguments in _allArguments)
178182
{
179183
// Don't start subsequent processes if we've been requested to cancel.
@@ -326,7 +330,7 @@ protected virtual void Dispose(bool disposing)
326330
protected int _numCores;
327331

328332
private System.Threading.Thread _thread = null;
329-
private bool _terminateThread = false;
333+
private AtomicBool _terminateThread = new AtomicBool(false);
330334
private List<string> _allArguments;
331335
}
332336
}

0 commit comments

Comments
 (0)