icsharpcode / RefactoringEssentials

Refactoring Essentials for Visual Studio
MIT License
638 stars 119 forks source link

ThreadStateException on ClipBoard.SetText() #302

Open InteXX opened 7 years ago

InteXX commented 7 years ago

After updating to v4.8 from the original v4.4 that I installed mid-Nov 2016, I'm getting this error when converting:

---------------------------
Microsoft Visual Studio
---------------------------
Convert C# to VB:

An error has occurred during conversion: System.Threading.ThreadStateException: 

Current thread must be set to single thread apartment (STA) mode before OLE calls can be made.
  at System.Windows.OleServicesContext.SetDispatcherThread()
  at System.Windows.OleServicesContext.get_CurrentOleServicesContext()
  at System.Windows.Clipboard.CriticalSetDataObject(Object data, Boolean copy)
  at System.Windows.Clipboard.SetDataInternal(String format, Object data)
  at System.Windows.Clipboard.SetText(String text, TextDataFormat format)
  at System.Windows.Clipboard.SetText(String text)
  at RefactoringEssentials.VsExtension.CodeConversion.PerformCSToVBConversion(IServiceProvider serviceProvider, String inputCode) in C:\projects\refactoringessentials\Vsix\CodeConversion.cs:line 55
  at RefactoringEssentials.VsExtension.ConvertCSToVBCommand.<ProjectItemMenuItemCallback>d__16.MoveNext() in C:\projects\refactoringessentials\Vsix\ConvertCSToVBCommand.cs:line 148

I'm running VS 2015.3.

Things I've tried:

Oddly, I can successfully make the same ClipBoard.SetText() call, referencing the same Framework assemblies, with a quick VB.NET console program of my own.

In the end, the only thing that has worked for me has been to revert to the older v4.4 (I'm not finding a download for v4.6, and that version isn't showing up in my system backups). Accordingly, I've turned off auto-update in my VSIX settings.

It's doubtful that this manifests on your system, as you would have encountered it early in your tests. So I'm perplexed. I'm unaware of how to proceed. Please advise.

christophwille commented 7 years ago

@siegfriedpammer please verify https://github.com/icsharpcode/RefactoringEssentials/commit/15454bea78991cc718b7fe02a0b604f904383057 but I am sure that is the culprit (seems we went overboard with ConfigureAwait in this place)

siegfriedpammer commented 7 years ago

The error message is a pretty strong hint, that it's related to ConfigureAwait. Oddly enough, I cannot reproduce the issue at all (with and without your change).

InteXX commented 7 years ago

@siegfriedpammer — I'm not very familiar with Async/Await, nor Threading in general, but I've seen comments on SO to the effect that Async/Await doesn't use threading. Stephen (Steven?) Cleary is a pretty good authority on Async/Await.

christophwille commented 7 years ago

https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.configureawait(v=vs.110).aspx

That's why it might or might not work, depending on your "luck".

InteXX commented 7 years ago

@christophwille — Thanks, I stand corrected.