William Yue
William Yue

Reputation: 1

What's System.Threading.ThreadStateException and how to avoid it

When I copied a program from a winForm textbook and tried to run it on Visual Studio, the compiler threw this exception, the wrong function is below and it shows me this:

System.Threading.ThreadStateException
HResult=0x80131520
Message=Before you can make OLE calls, you must set the current thread to single-threaded apartment (STA) mode. Make sure your Main function is marked with the STAThreadAttribute. This exception is only raised if a debugger is attached to the process.
Source=System.Windows.Forms StackTrace: at System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner) at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner) at MyNamespace.MyForm.OnLoadClick(Object sender, EventArgs e) in E:\Visual Studio Projects\Winform\Program.cs: line 37 at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m ) in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) in System.Windows.Forms.NativeWindow.Callback(HWND hWnd, MessageId msg, WPARAM wparam, LPARAM lparam)

The code:

private void OnLoadClick(object sender, System.EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Title = "Open Photo";
    dlg.Filter = "png files (*.png)|*.png|All files (*.*)|*.*";
    ***if (dlg.ShowDialog() == DialogResult.OK)***  //that's the wrong place
    {
        pboxPhoto.Image = new Bitmap(dlg.OpenFile());
    }
    dlg.Dispose();
}

All I want to know is why this error occurs.

I follow the tios and add [STKThread] before the Main function and it solved.But I'm still confused why and this program is copied from a book.That's the whole code

namespace MyNamespace
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    public class MyForm : System.Windows.Forms.Form
    {
        Button btnLoad;
        PictureBox pboxPhoto;
        public MyForm()
        {
            this.Text = "Hello Form 1.3";
            // Create and configure the Button
            btnLoad = new Button();
            btnLoad.Text = "&Load";
            btnLoad.Left = 10;
            btnLoad.Top = 10;
            btnLoad.Click += new System.EventHandler(OnLoadClick);
            // Create and configure the PictureBox
            pboxPhoto = new PictureBox();
            pboxPhoto.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            pboxPhoto.Width = this.Width / 3;
            pboxPhoto.Height = this.Height / 3;
            pboxPhoto.Left = (this.Width - pboxPhoto.Width) / 2;
            pboxPhoto.Top = (this.Height - pboxPhoto.Height) / 2;
            pboxPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
            // Add our new controls to the Form
            this.Controls.Add(btnLoad);
            this.Controls.Add(pboxPhoto);
        }
        private void OnLoadClick(object sender, System.EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Open Photo";
            dlg.Filter = "png files (*.png)|*.png|All files (*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                pboxPhoto.Image = new Bitmap(dlg.OpenFile());
            }
            dlg.Dispose();
        }
       // [STAThread]
        public static void Main()
        {
            Application.Run(new MyForm());
        }
    }
}

Upvotes: 0

Views: 24

Answers (0)

Related Questions