0

I get threadstateexception was unhandled whenever I try to open a file dialogue. I only get it when running Visual studio(2012) with Release setting, when I change back to Debug everything seems to work as intended. The application crashed on the last line, ShowDialog();

What am I doing wrong? Is there an code error or are there some settings in VS I'm missing?

I have copied all subfiles I'm using from Debug to release it that matters. I've tried searching for this issue, but my Google-fu is lacking.

Some cropped Code:

[STAThread]
public static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

public static bool compareListViewWithFile(int listViewIndex)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "Comma Separated Value files (.csv)|*.csv|All Files (*.*)|*.*";
    openFileDialog1.FilterIndex = 1;

    if (currentListView.Items == null || currentListView.Items.Count == 0)
    {
        openFileDialog1.Title = "Choose first file";
        DialogResult userClickedOK = openFileDialog1.ShowDialog();
        // etc
    }
}
Sid M
  • 4,354
  • 4
  • 30
  • 50
Fousk
  • 31
  • 4
  • 1
    Probably your Debug configuration is wrong as well, this is undefined behavior. Add line `Control.CheckForIllegalCrossThreadCalls = true;` to the beginning of Main function to check this. – Alex F Jun 02 '14 at 13:55
  • @Alex I have added the Control.CheckForIllegalCrossThreadCalls = true; in Main, but I don't see any difference. – Fousk Jun 03 '14 at 06:01

0 Answers0