I have forms: Splashscreen
, Search
, and NewEntry
Program.cs
starts Splashscreen
which checks some parameters and upon success (check user name, permissions, etc) opens Search
form. From there I can call the NewEntry
form.
So since the Splashscreen
is the primary form called by the Main()
function, once it closes, the app closes (this is the expected behaviour).
So what I did is from within Splashscreen
I start the Search
form as a dialog and hide Splashscreen
, so it waits for the Search
to close. As soon as it closes, I also close the Splashscreen
and it all seemed to make sense at the time. From the Search
form I open the NewEntry
form (from a button click) also as a dialog (I don't want users clicking back and forth and creating multiple NewEntry
windows).
Today I had to add a feature that allows the user to select a file. As an obvious choice, I used an OpenFileDialog
. But as soon as I call .ShowDialog()
method, the whole app just freezes.
I've read Windows Forms GUI hangs when calling OpenFileDialog.ShowDialog() and OpenFileDialog.ShowDialog() freezing application c# and a bunch of other posts here and on other sites, and when I almost lost hope, I came across this http://wishmesh.com/2011/06/call-to-openfiledialog-or-savefiledialog-hangs-or-freezes/
I do have the [STAThread]
attribute set on my Main()
function
And one particular point of interest is: For OpenFileDialog the ShowHelp property must be explicitly set.
And also ... They don’t have to be set to true, they just have to be initialized to either true or false.
So when I set the ShowHelp
to true
, the dialog shows up (with the useless Help button).
A further research showed that when I execute (new OpenFileDialog()).ShowDialog();
within Program.cs
or within Splashscreen
, it works just fine; however, when called from a dialog, there is a hang (without the ShowHelp
). Furthermore, a MessageBox
shows up just fine from the Dialog
...
So is there a way to make this work? Or should I manage my windows differently?
For example, have Search
as the main startup window, then before the window shows, call Splashscreen
as a dialog, and if it fails, just close the main window? But then, how would I handle the NewEntry
to be able to show OpenFileDialog
or FolderBrowserDialog
?
Thanks.