codemaid icon indicating copy to clipboard operation
codemaid copied to clipboard

Exception when cleaning up all code: InsertFileHeaderDocumentStart must be called on the UI thread

Open FStapenhorst opened this issue 4 years ago • 3 comments

Environment

  • Visual Studio version: 2022 Professional 17.0.0
  • CodeMaid version: 12.0
  • Code language: C#

Description

I run "Clean all code" and I get this exception for almost every file:

[CodeMaid Warning 08:10:56 ] Activation was not completed before cleaning began for 'Archive.cs'
[CodeMaid Handled Exception 08:10:59 ] CodeMaid Cleanup for 'Archive.cs' was stopped: System.Runtime.InteropServices.COMException (0x8001010E): InsertFileHeaderDocumentStart must be called on the UI thread.
   at Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread(String callerMemberName)
   at SteveCadwallader.CodeMaid.Logic.Cleaning.FileHeaderLogic.InsertFileHeaderDocumentStart(TextDocument textDocument, String settingsFileHeader) in C:\projects\codemaid\CodeMaidShared\Logic\Cleaning\FileHeaderLogic.cs:line 166
   at SteveCadwallader.CodeMaid.Logic.Cleaning.CodeCleanupManager.RunCodeCleanupCSharp(Document document) in C:\projects\codemaid\CodeMaidShared\Logic\Cleaning\CodeCleanupManager.cs:line 264
   at SteveCadwallader.CodeMaid.Logic.Cleaning.CodeCleanupManager.<>c__DisplayClass19_0.<Cleanup>b__0() in C:\projects\codemaid\CodeMaidShared\Logic\Cleaning\CodeCleanupManager.cs:line 170
   at SteveCadwallader.CodeMaid.Helpers.UndoTransactionHelper.Run(Action tryAction, Action`1 catchAction) in C:\projects\codemaid\CodeMaidShared\Helpers\UndoTransactionHelper.cs:line 55

FStapenhorst avatar Nov 09 '21 07:11 FStapenhorst

Thanks for reporting the issue. I haven't been able to reproduce it but I suspect that it may need the same UIThread.Run logic that was added here: https://github.com/codecadwallader/codemaid/blob/dev/CodeMaidShared/Helpers/TextDocumentHelper.cs#L72 to be added in places like here: https://github.com/codecadwallader/codemaid/blob/dev/CodeMaidShared/Logic/Cleaning/FileHeaderLogic.cs#L164

codecadwallader avatar Nov 16 '21 13:11 codecadwallader

Thanks. Let me know if I can do anything to help reproducing the issue.

FStapenhorst avatar Nov 16 '21 13:11 FStapenhorst

It looks like it it only happens if I have configured the setting "File header insert": Screenshot 2021-11-17 at 18 54 31 If I remove it, I do not get the exceptions.

FStapenhorst avatar Nov 17 '21 17:11 FStapenhorst