Skip to content

Commit f270844

Browse files
committed
Update WorkbookIntelliSenseProvider initialization
Use an ExcelSynchronizationContext for initialization code that needs to use the Excel COM API. This fixes a sporadic crash that would happen when calling Application.Run from another add-in with this add-in also loaded.
1 parent 37de2e4 commit f270844

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Source/ExcelDna.IntelliSense/Providers/WorkbookIntelliSenseProvider.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,15 @@ public IEnumerable<FunctionInfo> GetFunctionInfos()
122122
}
123123

124124
Dictionary<string, WorkbookRegistrationInfo> _workbookRegistrationInfos = new Dictionary<string, WorkbookRegistrationInfo>();
125+
ExcelSynchronizationContext _syncContextExcel;
125126
XmlIntelliSenseProvider _xmlProvider;
126127
public event EventHandler Invalidate;
127128

128129
#region IIntelliSenseProvider implementation
129130

130131
public WorkbookIntelliSenseProvider()
131132
{
133+
_syncContextExcel = new ExcelSynchronizationContext();
132134
_xmlProvider = new XmlIntelliSenseProvider();
133135
_xmlProvider.Invalidate += ( sender, e) => OnInvalidate();
134136
}
@@ -137,22 +139,29 @@ public void Initialize()
137139
{
138140
Logger.Provider.Info("WorkbookIntelliSenseProvider.Initialize");
139141
_xmlProvider.Initialize();
142+
_syncContextExcel.Post(OnInitialize, null);
143+
}
144+
145+
// Must be called on the main thread, in a macro context
146+
void OnInitialize(object _unused_)
147+
{
148+
Logger.Provider.Info("WorkbookIntelliSenseProvider.OnInitialize");
140149

141150
// The events are just to keep track of the set of open workbooks,
142151
var xlApp = (Application)ExcelDnaUtil.Application;
143152
xlApp.WorkbookOpen += Excel_WorkbookOpen;
144153
xlApp.WorkbookBeforeClose += Excel_WorkbookBeforeClose;
145154
xlApp.WorkbookAddinInstall += Excel_WorkbookAddinInstall;
146155
xlApp.WorkbookAddinUninstall += Excel_WorkbookAddinUninstall;
147-
Logger.Provider.Verbose("WorkbookIntelliSenseProvider.Initialize - Installed event listeners");
156+
Logger.Provider.Verbose("WorkbookIntelliSenseProvider.OnInitialize - Installed event listeners");
148157

149158
lock (_workbookRegistrationInfos)
150159
{
151-
Logger.Provider.Verbose("WorkbookIntelliSenseProvider.Initialize - Starting Workbooks loop");
160+
Logger.Provider.Verbose("WorkbookIntelliSenseProvider.OnInitialize - Starting Workbooks loop");
152161
foreach (Workbook wb in xlApp.Workbooks)
153162
{
154163
var name = wb.Name;
155-
Logger.Provider.Verbose($"WorkbookIntelliSenseProvider.Initialize - Adding registration for {name}");
164+
Logger.Provider.Verbose($"WorkbookIntelliSenseProvider.OnInitialize - Adding registration for {name}");
156165
if (!_workbookRegistrationInfos.ContainsKey(name))
157166
{
158167
WorkbookRegistrationInfo regInfo = new WorkbookRegistrationInfo(name);
@@ -195,7 +204,7 @@ public void Initialize()
195204
// }
196205
//}
197206

198-
Logger.Provider.Verbose($"WorkbookIntelliSenseProvider.Initialize - Checking Add-Ins");
207+
Logger.Provider.Verbose($"WorkbookIntelliSenseProvider.OnInitialize - Checking Add-Ins");
199208

200209
var loadedAddIns = Integration.XlCall.Excel(Integration.XlCall.xlfDocuments, 2) as object[,];
201210
if (loadedAddIns == null)

0 commit comments

Comments
 (0)