Exception

P

PiotrKolodziej

Hi.
I have hugie problem with exception. I need to debug button1_Click code,
after openfiledialog is called but i can't because of this:
System.Threading.ThreadStateException was unhandled
Message="Current thread must be set to single thread apartment (STA) mode
before OLE calls can be made. Ensure that your Main function has
STAThreadAttribute marked on it. This exception is only raised if a debugger
is attached to the process."


Here is few samples of my code:

namespace IRDAcontrol
{
public partial class Form2 : Form
{
public string Button = null;

public Form2()
{
InitializeComponent();

comboBox1.Items.Add("Volume UP");
comboBox1.Items.Add("Volume DN");
comboBox1.Items.Add("Mute");
comboBox1.Items.Add("<Custom>");

}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Button + " " + openFileDialog1.FileName);

IRDAcontrol.configFile.setParameter(Button,
openFileDialog1.FileName);
this.Dispose();
}

private void button2_Click(object sender, EventArgs e)
{
DialogResult asd = openFileDialog1.ShowDialog();
}
}
}



AND this is main:


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace IRDAcontrol
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}


For any help thanks.
 
J

Jon Skeet [C# MVP]

PiotrKolodziej said:
I have hugie problem with exception. I need to debug button1_Click code,
after openfiledialog is called but i can't because of this:
System.Threading.ThreadStateException was unhandled
Message="Current thread must be set to single thread apartment (STA) mode
before OLE calls can be made. Ensure that your Main function has
STAThreadAttribute marked on it. This exception is only raised if a debugger
is attached to the process."

You've shown Form2, but Form1 is what's being started in Main. Is Form2
being run in a different thread, perhaps?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top