Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

System.InvalidOperationException. The thread must be STA
CodeAdze
#1 Posted : Saturday, March 12, 2016 11:51:22 AM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2014(UTC)
Posts: 29
Location: Australia

Thanks: 4 times
Was thanked: 3 time(s) in 3 post(s)
Hi,
I have the following test fixture.
Code:

Imports System.Windows.Controls
Imports NUnit.Framework


<TestFixture>
Public Class SearchResultPaserTests
  <Test>
  <STAThread>
  Public Sub ParseMethodReturnsComboBoxItem()
    Dim srp As New SearchResultPaser

    Assert.AreEqual(GetType(ComboBoxItem), srp.Parse(New SearchResult()).GetType())
  End Sub
End Class


Which tests the following class which is in a class library referenced by a WPF application.

Code:

Imports System.Windows.Controls

Public Class SearchResultPaser
  <STAThread>
  Public Function Parse(result As SearchResult) As ComboBoxItem

   [h] Return New ComboBoxItem()[/h]
  End Function
End Class


I am getting a "Invalid Operation Exception : The calling thread must be STA, because many UI components require this." when I try to return a new ComboBoxItem . After reading this post I tried adding the <STAThread> attributes but still no joy.
The SearchResultPaser class works fine when called form my WPF app presumably because it is then running on WPF's UI thread.

What can I do to resolve this.

Thanks
Ralf Koban
#2 Posted : Saturday, March 12, 2016 4:37:37 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 5/19/2014(UTC)
Posts: 44
Location: Germany

Thanks: 4 times
Was thanked: 10 time(s) in 9 post(s)
Hi,

seems you are using NUnit 3 where STAThread is no longer supported.

Please use the Apartment state instead:

<TestFixture>
Public Class SearchResultPaserTests
<Test>
<Apartment(ApartmentState.STA)>
Public Sub ParseMethodReturnsComboBoxItem()
Dim srp As New SearchResultPaser

Assert.AreEqual(GetType(ComboBoxItem), srp.Parse(New SearchResult()).GetType())
End Sub
End Class


Regards
2 users thanked Ralf Koban for this useful post.
Remco on 3/12/2016(UTC), CodeAdze on 3/13/2016(UTC)
CodeAdze
#3 Posted : Sunday, March 13, 2016 9:58:12 AM(UTC)
Rank: Member

Groups: Registered
Joined: 9/3/2014(UTC)
Posts: 29
Location: Australia

Thanks: 4 times
Was thanked: 3 time(s) in 3 post(s)
Hi Ralf,
Thanks for your help with this, yes I was using NUnit 3 which I forgot to mention in my post and your suggestion to change <STAThread> to <Apartment(ApartmentState.STA)> worked perfectly.

Thanks again!
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.026 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download