Home » excel » vb.net – Slow Excel Shutdown When WPF Called from Add-In

vb.net – Slow Excel Shutdown When WPF Called from Add-In

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have an Excel add-in with a button on it that calls a WPF application on a new thread. When I close Excel not having opened my WPF application or after opening it and then closing it again, Excel closes immediately, however, whenever I open the application and then close Excel, Excel takes 5-10 seconds to close. I’ve only come across these solutions, neither of which has helped:

I’m running VS 2010 and Excel 2010, so there shouldn’t be an interoperability problem.

Does anyone have a suggestion?

Thread Code:

Private qbdThread As Thread = Nothing
Private frmQBD As QBDApplication.MainWindow

qbdThread = New Thread(New ParameterizedThreadStart(AddressOf RunQBD))
qbdThread.SetApartmentState(Threading.ApartmentState.STA)
qbdThread.Start(TabletType)
AddHandler QBDApplication.MainWindow.QBDClose, AddressOf QBDThreadClose

Private Sub RunQBD(Optional tabletQBDSelected As String = Nothing)
...

frmQBD = New QBDApplication.MainWindow(contacts, saveLocation, tabletQBDLocal)
frmQBD.Show()
frmQBD.Activate()
System.Windows.Threading.Dispatcher.Run()
End Sub

This code runs when the app is closed by the user on the new thread:

Me.Close()
System.Windows.Threading.Dispatcher.CurrentDispatcher.InvokeShutdown()

An event is then raised on the main thread (ThisAddin.vb) with this code:

Private Sub QBDThreadClose()
    qbdThread = Nothing
    frmQBD = Nothing
End Sub

One other thing to note is that when frmQBD is not created as a class variable, and instead dimensioned in the “RunQBD” sub, this issue does not occur. This would solve my problem, but then I wouldn’t be able to access something like frmQBD.Activate() on the main thread, which I need to be able to do.

EDIT: Code has been updated

How to&Answers:

We found a solution for this problem.

You have to set an AppSwith in the framework

Public Sub EnablePointerSupport()
AppContext.SetSwitch("Switch.System.Windows.Input.Stylus.EnablePointerSupport",True)
End Sub

You can find more information about it here and here.

Answer:

This Microsoft bug is described at https://connect.microsoft.com/VisualStudio/feedback/details/783019/word-slow-shutdown-on-windows-8-when-using-wpf-in-application-addin. It was initially reported in 2013 and supposedly fixed in 2014, but appears to be back now.

We can reproduce this exact problem with Excel and Word 2016 running on Windows 10, but mysteriously not in PowerPoint. Latest version of VSTO Runtime is installed. We could not reproduce the problem in Office 2013 running on Windows 8.1.

The workaround is as described at the link above–go to Device Manager > Human Interface Devices and disable “HID-compliant touch screen” (in our testing) or perhaps another one of the “HID-compliant” items.