diff --git a/ILSpy.AddIn/Commands/NuGetReferenceForILSpy.cs b/ILSpy.AddIn/Commands/NuGetReferenceForILSpy.cs
index bcbaa82a3..84ee74af5 100644
--- a/ILSpy.AddIn/Commands/NuGetReferenceForILSpy.cs
+++ b/ILSpy.AddIn/Commands/NuGetReferenceForILSpy.cs
@@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EnvDTE;
+using Microsoft.VisualStudio.Shell;
namespace ICSharpCode.ILSpy.AddIn.Commands
{
@@ -26,6 +27,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
/// instance or null, if item is not a supported project.
public static NuGetReferenceForILSpy Detect(object itemData)
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
if (itemData is ProjectItem projectItem) {
var properties = Utils.GetProperties(projectItem.Properties, "Type");
if ((properties[0] as string) == "Package") {
@@ -42,6 +45,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
/// Parameters object or null, if not applicable.
public ILSpyParameters GetILSpyParameters()
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
var properties = Utils.GetProperties(projectItem.Properties, "Name", "Version", "Path");
if (properties[0] != null && properties[1] != null && properties[2] != null) {
return new ILSpyParameters(new[] { $"{properties[2]}\\{properties[0]}.{properties[1]}.nupkg" });
diff --git a/ILSpy.AddIn/Commands/OpenCodeItemCommand.cs b/ILSpy.AddIn/Commands/OpenCodeItemCommand.cs
index 0f26fa2d6..17476d109 100644
--- a/ILSpy.AddIn/Commands/OpenCodeItemCommand.cs
+++ b/ILSpy.AddIn/Commands/OpenCodeItemCommand.cs
@@ -23,6 +23,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
protected override void OnBeforeQueryStatus(object sender, EventArgs e)
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
if (sender is OleMenuCommand menuItem) {
menuItem.Visible = false;
@@ -46,6 +48,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
Document GetRoslynDocument()
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
var document = owner.DTE.ActiveDocument;
var selection = (EnvDTE.TextPoint)((EnvDTE.TextSelection)document.Selection).ActivePoint;
var id = owner.Workspace.CurrentSolution.GetDocumentIdsWithFilePath(document.FullName).FirstOrDefault();
@@ -57,12 +61,16 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
EnvDTE.TextPoint GetEditorSelection()
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
var document = owner.DTE.ActiveDocument;
return ((EnvDTE.TextSelection)document.Selection).ActivePoint;
}
protected override async void OnExecute(object sender, EventArgs e)
{
+ await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
+
var textView = Utils.GetCurrentViewHost(owner)?.TextView;
if (textView == null)
return;
@@ -89,6 +97,7 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
// Add our own project as well (not among references)
var project = FindProject(owner.DTE.Solution.Projects.OfType(), roslynProject.FilePath);
+
if (project == null) {
owner.ShowMessage(OLEMSGICON.OLEMSGICON_WARNING, "Can't show ILSpy for this code element!");
return;
diff --git a/ILSpy.AddIn/Commands/OpenILSpyCommand.cs b/ILSpy.AddIn/Commands/OpenILSpyCommand.cs
index 35a2cc29f..acb7b12b9 100644
--- a/ILSpy.AddIn/Commands/OpenILSpyCommand.cs
+++ b/ILSpy.AddIn/Commands/OpenILSpyCommand.cs
@@ -55,6 +55,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
protected void OpenAssembliesInILSpy(ILSpyParameters parameters)
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
if (parameters == null)
return;
@@ -71,6 +73,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
protected Dictionary GetReferences(Microsoft.CodeAnalysis.Project parentProject)
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
var dict = new Dictionary();
foreach (var reference in parentProject.MetadataReferences) {
using (var assemblyDef = AssemblyDefinition.ReadAssembly(reference.Display)) {
@@ -97,6 +101,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
protected EnvDTE.Project FindProject(IEnumerable projects, string projectFile)
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
foreach (var project in projects) {
switch (project.Kind) {
case DTEConstants.vsProjectKindSolutionItems:
diff --git a/ILSpy.AddIn/Commands/OpenProjectOutputCommand.cs b/ILSpy.AddIn/Commands/OpenProjectOutputCommand.cs
index 8fd06a50c..937d1601a 100644
--- a/ILSpy.AddIn/Commands/OpenProjectOutputCommand.cs
+++ b/ILSpy.AddIn/Commands/OpenProjectOutputCommand.cs
@@ -17,6 +17,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
protected override void OnBeforeQueryStatus(object sender, EventArgs e)
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
if (sender is OleMenuCommand menuItem) {
menuItem.Visible = false;
@@ -27,6 +29,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
protected override void OnExecute(object sender, EventArgs e)
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
if (owner.DTE.SelectedItems.Count != 1)
return;
var projectItemWrapper = ProjectItemForILSpy.Detect(owner, owner.DTE.SelectedItems.Item(1));
diff --git a/ILSpy.AddIn/Commands/OpenReferenceCommand.cs b/ILSpy.AddIn/Commands/OpenReferenceCommand.cs
index dd6ab3581..e97a2af35 100644
--- a/ILSpy.AddIn/Commands/OpenReferenceCommand.cs
+++ b/ILSpy.AddIn/Commands/OpenReferenceCommand.cs
@@ -22,6 +22,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
protected override void OnBeforeQueryStatus(object sender, EventArgs e)
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
if (sender is OleMenuCommand menuItem) {
menuItem.Visible = false;
@@ -44,6 +46,8 @@ namespace ICSharpCode.ILSpy.AddIn.Commands
protected override void OnExecute(object sender, EventArgs e)
{
+ ThreadHelper.ThrowIfNotOnUIThread();
+
var itemObject = owner.GetSelectedItemsData