'OpenFileDialog crashes for no reason?

  • Thread starter Thread starter Adam Honek
  • Start date Start date
A

Adam Honek

This really is my unlucky day it seems.

I'm getting an unhandled exception error on the below:

I've changed it without the Me and not a chance, still crashes the app.

All I want to do is get a file name hence file path of a selected file.

Thanks,
Adam

'OpenFileDialog1.InitialDirectory = Application.StartupPath

'OpenFileDialog1.ShowDialog(Me)
 
I don't get the bug with FontDialog1.ShowDialog()

This leads me to think it's something that I haven't initialized the
'OpenFileDialog1 before showing it????

Can't think of anything else at the moment.

Adam
 
This is the error it tells me:

An unhandled exception of type 'System.Threading.ThreadStateException'
occurred in System.Windows.Forms.dll

Additional information: 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.

All item dialogs word (ie color, font). All in the same thread too.

Adam
 
I checked in my other forms and in the main form it doesn't crash.

OpenFileDialog1.ShowDialog()

As others are in there own threads this leads me to believe it's something
to do with threading.

Still don't get why the ColorDialog and FontDialog all work fine in ALL
forms threaded or not but this one just refuses to listen.

Very puzzling.

What else can I do? Is this a bug, seems a pretty severe one, OpenDialog is
a very often used control.

Thanks,
Adam
 
Adam Honek said:
An unhandled exception of type 'System.Threading.ThreadStateException'
occurred in System.Windows.Forms.dll

Additional information: 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.

Does your 'Sub Main' have the 'MTAThread' attribute specified? If this is
the case, either remove the attribute or change it to 'STAThread'.
 
My app doesn't start in Sub Main() but rather frmMain_Load().

If I change it all the eye candy Windows XP GUI features get
disabled hence I keep it on.

Adam
 
Adam Honek said:
My app doesn't start in Sub Main() but rather frmMain_Load().

If I change it all the eye candy Windows XP GUI features get
disabled hence I keep it on.

Hmmm... You can add Windows XP Visual Styles as follows:

\\\
Application.EnableVisualStyles()
Application.Run(New MainForm())
///
 
I'll try this later on in the evening.

I'm sure all MS dialog controls use inheritance hence the openfiledialog
must be a version of say fontdialog etc.

Do you think entering a critical section is something that might be viable?

It's obviously a thread error that manifests itself if openfiledialog is not
called from the main app's thread.

Do others get the same bug, a simply new project with a 2nd thread should be
enough to test it out?

VS 2005 here.

Adam
 
Back
Top