I'm encountering an InvalidOperationException with the message "The calling thread must be STA, because many UI components require this." in a WPF application with heavy dependence on a referenced library.
I've tried to pin down where the error is coming from, using dispatchers of various threads and objects, ensured main() has STAthread attribute, tried setting "[STAThread]" on seemingly relevant methods.
Inside MyParticipant constructor, as MyVideoRenderer pic is being constructed, which inherits VideoRenderer, the VideoRenderer constructor itself is throwing this exception, not entering the constructor.
Code:
public class MyParticipant : Participant //inside MainWindow.xaml.cs
{
public enum PictureMode
{
Avatar,
Video
}
public PictureMode pictureMode = PictureMode.Avatar;
public ProgressBar voiceVolume;
public Label nameLabel;
public MyVideoRenderer pic;
public MyVideo video;
public bool isCachedInClient = false;
public string displayName = null;
public Image avatarImage = null;
public static int picHeight = 480;
public static int piclWidth = 640;
public static int panelHeight = 155;
public static int panelWidth = 174;
public static Color liveColor = SystemColors.GradientActiveCaptionColor;
public static Color nonLiveColor = SystemColors.GradientInactiveCaptionColor;
public MyParticipant(uint objectId, VideoManager videoManager)
: base(objectId, videoManager)
{
pic = new MyVideoRenderer(videoManagerRef)
{
//Top = 5,
//Left = 5,
Height = picHeight,
Width = piclWidth,
//SizeMode = PictureBoxSizeMode.StretchImage
};
...
public class VideoRenderer : System.Windows.Controls.Image //referenced external class
{
public VideoRenderer(VideoManagerRoot videoManager) ///Exception here
{
this.videoManagerRef = videoManager;
}
...