1

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;
    }
...
user1518816
  • 215
  • 3
  • 12
  • 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 Jul 11 '12 at 20:49

3 Answers3

8

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

Read:

Community
  • 1
  • 1
Rafal
  • 12,391
  • 32
  • 54
  • Thank you, Rafal. The first link helped a ton with understanding the problem: 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. MTA thread constructing a UI control results in this exception. – user1518816 Jul 12 '12 at 14:34
0

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.

user1518816
  • 215
  • 3
  • 12
0

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

Roske
  • 1