RefactoringEssentials icon indicating copy to clipboard operation
RefactoringEssentials copied to clipboard

ThreadStateException on ClipBoard.SetText()

Open InteXX opened this issue 8 years ago • 5 comments

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:

  • Restarting Visual Studio
  • Signing out and back in
  • Restarting the computer
  • Uninstalling and reinstalling the Add-In

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.

InteXX avatar Mar 29 '17 23:03 InteXX

@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)

christophwille avatar Mar 30 '17 05:03 christophwille

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).

siegfriedpammer avatar Mar 30 '17 07:03 siegfriedpammer

@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.

InteXX avatar Mar 30 '17 07:03 InteXX

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".

christophwille avatar Mar 30 '17 07:03 christophwille

@christophwille — Thanks, I stand corrected.

InteXX avatar Mar 30 '17 07:03 InteXX