Excel automatically reboots and/or will not shut down

46 zobrazení
Preskočiť na prvú neprečítanú správu

Terry Aney

neprečítané,
21. 11. 2022, 7:37:1321. 11. 2022
komu: Excel-DNA
Looking for tips on where to even start debugging or looking at logs.

I can't remember when it started, but it has been a while, but I have 2 strange behaviors.

First, about 20% of the time I (and users of my addin) get the following flow:

1. I close Excel, the window closes, but the process doesn't close **
2. When I re-open Excel, my addin ribbon does not display at all
3. If I go to the AddIns dialog, my addin is still selected, if I unselect it and close the dialog Excel GPFs and disappears without a visible message.
4. If I restart Excel, I get the following message about my addin:

Excel is running into problems with the 'c:\program files\microsoft office\root\office16\library\rbletools\btr.extensibility.excel64.xll' add-in. If this keeps happening, disable this add-in and check for available updates. Do you want to disable it now?

5. I say no, Excel then opens and my addin is displayed and functional.
6. If I close Excel, everything shuts down fine.

** If I look in task manager after closing and Excel is still running, and I terminate the process, everything works fine the next time I open.

Secondly, when debugging, about 20% of the time (again, lol) when I close Excel to stop the debug session, Excel automatically just starts back up on its own.

I hook into the following events, but don't do anything drastic (in my opinion) :)

application.WorkbookOpen += Application_WorkbookOpen;
application.WorkbookBeforeSave += Application_WorkbookBeforeSave;
application.WorkbookAfterSave += Application_WorkbookAfterSave;
application.WorkbookActivate += Application_WorkbookActivate;
application.WorkbookDeactivate += Application_WorkbookDeactivate;
application.SheetActivate += Application_SheetActivate;
application.SheetDeactivate += Application_SheetDeactivate;
application.SheetChange += Application_SheetChange;

Obviously there is something I must be doing, but I don't know if there are some logs I can review or events I can hook into to generate some useful logs.  Any tips would be greatly appreciated.  Let me know if I can provide any other useful information.

Kedar Kulkarni

neprečítané,
21. 11. 2022, 12:16:0621. 11. 2022
komu: Excel-DNA
I have had something similar happen to me. Following is what I did. Try to figure out a way to reproduce the issue consistently with minimum steps.

The behavior you see is because of the following.
1. Open excel => addin loads
2. Close excel => Window closes but the process does not.
3. Starting excel again starts just opens /activates the previous excel process which does not have the addin loaded.

To fix the issue
1. Reproduce the issue with minimum steps.
2. Add break point at ribbon constructor - this usually is the starting point. 
3. Step through the code and make sure if you are accessing excel application / range / workbook / worksheet, it must be done via main thread. Anywhere you are starting a task or Async methods, please add a breakpoint and make sure the thread in the visual studio shows main thread for excel.

In my case, I was changing worksheet from a background thread which was causing the issue. 

Try to use ExcelAsyncUtil.QueueAsMacro if on a background thread to access the excel objects.

hope this helps,
Thanks,
Kedar

Govert van Drimmelen

neprečítané,
22. 11. 2022, 5:05:3422. 11. 2022
komu: Excel-DNA
Hi Terry,

I think Kedar is on the right track here.
All problems of the kind where Excel does not close properly seem to be causes by using the COM object model from another thread (not the main thread with ManagedThreadId == 1).
Any async functions or async work that you trigger from a macro, event or ribbon action must be very careful in how it calls the Excel object model.

In addition, when Excel is shutting down, you might find events that trigger in a state you don't expect - for example a sheet or workbook is deactivated, and then you might not be able to access it anymore. If you have some ExcelReference references that try to do something with the sheet after the shutdown starts, that might be a problem too. So you need to be quite careful and conscious of what you do in code that runs from events fired as Excel shuts down. I would make sure to put each of the event handlers in a try-catch block that will catch all exceptions - that won't quite catch everything, but in some situations leaking exceptions from managed code can rudely shut down the process.

-Govert
Odpovedať všetkým
Odpovedať autorovi
Poslať ďalej
0 nových správ