Search code examples
c#.netmultithreadingwinformsthread-exceptions

ThreadStateException occurred


I seem to have a problem with two lines of code in my program.

A ThreadStateException occurs at the lines if (o.ShowDialog() == DialogResult.OK) and if(s.ShowDialog() == DialogResult.OK)

The program is supposed to interpret a made up language, but that part of code has not been made yet. Please help, I have no idea what to do!

public class meow : Form
{
    TextBox meowbox = new TextBox();
    private string titile;
    public meow()
    {
        titile="Tiger goes Meow";
        Size = new Size(500, 600);
        Text =titile ;

        meowbox.Size = new Size(450, 520);
        meowbox.Multiline = true;
        meowbox.ScrollBars = ScrollBars.Horizontal;
        meowbox.WordWrap = true;
        meowbox.Location = new Point(25, 10);

        //file
        MenuItem feow = new MenuItem("File Meow");
        MenuItem oeow = new MenuItem("open Meow");
        MenuItem seow = new MenuItem("Save Meow");
        feow.MenuItems.Add(oeow);
        feow.MenuItems.Add(seow);
        //run
        MenuItem leow = new MenuItem("Meow");
        MenuItem ceow = new MenuItem("Check Meow");
        MenuItem reow = new MenuItem("Run Meow");
        leow.MenuItems.Add(ceow);
        leow.MenuItems.Add(reow);
        //menu
        MainMenu beow = new MainMenu();
        Menu = beow;
        beow.MenuItems.Add(feow);
        beow.MenuItems.Add(leow);
        //put it all meow
        Controls.Add(meowbox);
        //handlers
        oeow.Click += new EventHandler(oeow_Click);
        seow.Click += new EventHandler(seow_Click);
       /*ceow.Click += new EventHandler(ceow_Click);
         reow.Click += new EventHandler(reow_Click);*/
    }

    protected void oeow_Click( object sender, EventArgs e){
        Text="Oeow";
        OpenFileDialog o = new OpenFileDialog();
        if (o.ShowDialog() == DialogResult.OK)
        {
            Stream file = o.OpenFile();
            StreamReader reader = new StreamReader(file);
            char[] data = new char[file.Length];
            reader.ReadBlock(data, 0, (int)file.Length);
            meowbox.Text = new String(data);
            reader.Close();
        }
        Text = titile;
    }

    protected void seow_Click(object sender, EventArgs e)
    {
        Text="seow";
        SaveFileDialog s = new SaveFileDialog();
        if(s.ShowDialog() == DialogResult.OK)
        {
            StreamWriter writer = new StreamWriter(s.OpenFile());
            writer.Write(meowbox.Text);
            writer.Close();
        }
        Text=titile;
    }

    public static void Main()
    {
        Application.Run(new meow());
    }
}

Solution

  • If the code you posted is your real program, then the problem is that you aren't setting the thread's apartment state correctly. The main Winforms UI thread must be a single-threaded apartment thread.

    Try:

    [STAThread]
    public static void Main()
    {
        Application.Run(new meow());
    }
    

    Note that ideally, you should just create your Winforms project using the template built into Visual Studio. It will configure the thread correctly for you.