1
votes

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;
    }
...
3
It is entirely unclear on what thread this code might run. The exception however indicates strongly that it is not on the main thread, the one that displays UI.Hans Passant

3 Answers

8
votes

My guess is that you're creating UI elements from a background thread, which is the cause of the exception.

Read:

0
votes

Solved, thanks to Rafal's post:

The problem is that the thread that was creating a new MyParticipant was being default set to MTA, and so inside MyParticipant, that MTA thread was calling new VideoRenderer, which inherits an Image. An MTA thread constructing a UI control results in this exception.

0
votes

In (WPF Application) Project Properties make sure that Startup object is set to (Not Set). That solved problem in my case.