OpenFileDialog throws a Threading Exception
-
Okay what I'm trying to do is use the Dialog window to have the user find an Excel Database file (xls extension). I have searched Google, Msdn and the site. Error: 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. Code where OpenFileDialog is created:
private void import_db(object sender, EventArgs e) { OpenFileDialog search_for_file = new OpenFileDialog(); search_for_file.AddExtension = true; search_for_file.CheckFileExists = true; search_for_file.DefaultExt = "xls"; search_for_file.Multiselect = false; DialogResult result = search_for_file.ShowDialog(); string file_name = search_for_file.FileName; }
It throws the error when I get to the ShowDialog method. -
Okay what I'm trying to do is use the Dialog window to have the user find an Excel Database file (xls extension). I have searched Google, Msdn and the site. Error: 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. Code where OpenFileDialog is created:
private void import_db(object sender, EventArgs e) { OpenFileDialog search_for_file = new OpenFileDialog(); search_for_file.AddExtension = true; search_for_file.CheckFileExists = true; search_for_file.DefaultExt = "xls"; search_for_file.Multiselect = false; DialogResult result = search_for_file.ShowDialog(); string file_name = search_for_file.FileName; }
It throws the error when I get to the ShowDialog method.Dumb question: Does you Main function have a STAThread attribute?
[STAThread] public static void Main(string[] args) { ... }
-
Dumb question: Does you Main function have a STAThread attribute?
[STAThread] public static void Main(string[] args) { ... }