Skip to content

Commit 707df69

Browse files
committed
Unhook WinEvents on automation thread
(Trying to prevent Excel 2016 exit crash)
1 parent e353da5 commit 707df69

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

Source/ExcelDna.IntelliSense.Host/ExcelDna.IntelliSense.Host.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ xcopy "$(SolutionDir)\packages\ExcelDna.AddIn.0.33.9\tools\ExcelDna.xll" "$(Targ
7272
xcopy "$(TargetDir)ExcelDna.IntelliSense.Host-AddIn.dna*" "$(TargetDir)ExcelDna.IntelliSense.Host-AddIn64.dna*" /C /Y
7373
xcopy "$(SolutionDir)\packages\ExcelDna.AddIn.0.33.9\tools\ExcelDna64.xll" "$(TargetDir)ExcelDna.IntelliSense.Host-AddIn64.xll*" /C /Y
7474
"$(SolutionDir)\packages\ExcelDna.AddIn.0.33.9\tools\ExcelDnaPack.exe" "$(TargetDir)ExcelDna.IntelliSense.Host-AddIn.dna" /Y /O "$(TargetDir)ExcelDna.IntelliSense.xll"
75-
"$(SolutionDir)\packages\ExcelDna.AddIn.0.33.9\tools\ExcelDnaPack.exe" "$(TargetDir)ExcelDna.IntelliSense.Host-AddIn64.dna" /Y /O "$(TargetDir)ExcelDna.IntelliSense64.xll"</PostBuildEvent>
75+
"$(SolutionDir)\packages\ExcelDna.AddIn.0.33.9\tools\ExcelDnaPack.exe" "$(TargetDir)ExcelDna.IntelliSense.Host-AddIn64.dna" /Y /O "$(TargetDir)ExcelDna.IntelliSense64.xll"
76+
xcopy "$(TargetDir)ExcelDna.IntelliSense.Host-AddIn.xll.config" "$(TargetDir)ExcelDna.IntelliSense.xll.config"</PostBuildEvent>
7677
</PropertyGroup>
7778
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
7879
Other similar extension points exist, see Microsoft.Common.targets.

Source/ExcelDna.IntelliSense/UIMonitor/WinEvents.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,25 @@ public WinEventHook(WinEvent eventMin, WinEvent eventMax, SynchronizationContext
101101
// Is SetLastError used? - SetWinEventHook documentation does not indicate so
102102
throw new Win32Exception("SetWinEventHook failed");
103103
}
104-
Logger.WinEvents.Info($"SetWinEventHook success");
104+
Logger.WinEvents.Info($"SetWinEventHook success on thread {Thread.CurrentThread.ManagedThreadId}");
105105
}
106106

107107
// This runs on the Excel main thread - get off quickly
108108
void HandleWinEvent(IntPtr hWinEventHook, WinEvent eventType, IntPtr hWnd,
109-
int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
109+
int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
110110
{
111-
// CONSIDER: We might add some filtering here... maybe only interested in some of the window / event combinations
112-
_syncContextAuto.Post(OnWinEventReceived, new WinEventArgs(eventType, hWnd, idObject, idChild, dwEventThread, dwmsEventTime));
111+
try
112+
{
113+
if (disposedValue)
114+
return;
115+
116+
// CONSIDER: We might add some filtering here... maybe only interested in some of the window / event combinations
117+
_syncContextAuto.Post(OnWinEventReceived, new WinEventArgs(eventType, hWnd, idObject, idChild, dwEventThread, dwmsEventTime));
118+
}
119+
catch (Exception ex)
120+
{
121+
Logger.WinEvents.Warn($"HandleWinEvent Exception {ex}");
122+
}
113123
}
114124

115125
// Runs on our Automation thread (via SyncContext passed into the constructor)
@@ -128,13 +138,24 @@ protected virtual void Dispose(bool disposing)
128138
{
129139
if (!disposedValue)
130140
{
141+
Logger.WinEvents.Info($"WinEventHook Dispose on thread {Thread.CurrentThread.ManagedThreadId}");
131142
if (disposing)
132143
{
133144
// TODO: dispose managed state (managed objects).
134145
}
135-
136-
Logger.WinEvents.Info($"UnhookWinEvent");
137-
UnhookWinEvent(_hWinEventHook);
146+
_syncContextAuto.Post(winEventHook =>
147+
{
148+
try
149+
{
150+
Logger.WinEvents.Info($"UnhookWinEvent called on thread {Thread.CurrentThread.ManagedThreadId}");
151+
bool result = UnhookWinEvent((IntPtr)winEventHook);
152+
Logger.WinEvents.Info($"UnhookWinEvent result {result}");
153+
}
154+
catch (Exception ex)
155+
{
156+
Logger.WinEvents.Warn($"UnhookWinEvent Exception {ex}");
157+
}
158+
}, _hWinEventHook);
138159

139160
disposedValue = true;
140161
}

0 commit comments

Comments
 (0)