0

I have a C# application which has an icon in the system tray. When the user right-clicks on it, it will show a menu.

  1. open file
  2. exit

When the user clicks 1. open file, it will show (ShowDialog) a form with two buttons

  1. open file
  2. close

When the user clicks 1. open file, it will do

OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "All files (*.*)|*.*";
DialogResult result = openFile.ShowDialog(); // deadlock here
if (result == DialogResult.OK){
    // do some thing
}

At line 3 the application deadlocks

  1. User can NOT interact with the form, it does NOT respond
  2. OpenFileDialog does NOT show

Could you please help explain reason why this problem occurs?

Update Answer

  1. The form which create icon in system tray is called from another thread.
  2. I set: thread.SetApartmentState(ApartmentState.STA);, OpenFileDialog will show.
GSP
  • 574
  • 3
  • 7
  • 34
  • 2
    When opening a modal dialog like the OpenFileDialog it is normal that you cannot interact with the parent form. Or are you saying that is the OpenFileDialog named openFile that is freezed? – Steve Nov 04 '16 at 08:59
  • @Steve when show OpenFileDialog, user can NOT interact with form which called OpenFileDialog. And OpenFileDialog does NOT show. – GSP Nov 04 '16 at 09:02
  • 2
    A MODAL dialog is by design MODAL. It block everything in the form chain before the call. See http://stackoverflow.com/questions/2834799/winforms-programming-modal-and-non-modal-forms-problem If you are in a point of your code where you need a file input for your user, then there is no sense in continuing your code without that file. – Steve Nov 04 '16 at 09:07
  • 1
    So the real question is "Why does the OpenFileDialog not open?" – Manfred Radlwimmer Nov 04 '16 at 09:08
  • @ManfredRadlwimmer is you application win form or wpf application? – Samvel Petrosov Nov 04 '16 at 09:10
  • 1
    @GSP try to use the constructor overload that takes the owner form _DialogResult result = openFile.ShowDialog(this);_ Your OpenFileDialog is probably behind the main form window – Steve Nov 04 '16 at 09:13
  • Sounds like no message loop is running. Does your application ever call `Application.Run()` anywhere? – Christ A Nov 04 '16 at 09:17
  • 1
    Does your `Main()` method have the `[STAThread]` attribute? If not, try adding it. – Matthew Watson Nov 04 '16 at 09:18
  • Same problem here: http://stackoverflow.com/questions/32434984/c-sharp-openfiledialog-thread-start-but-dialog-not-shown – Thomas K Nov 04 '16 at 09:18
  • @ChristA `OpenFileDialog` will work from a Console app as long as the Main() method has a `[STAThread]` attribute. – Matthew Watson Nov 04 '16 at 09:20
  • In program.cs, I call `Application.Run(new MainForm(validArgs));`. I tried to use [STAThread]. but it does not work – GSP Nov 04 '16 at 09:20
  • If you try that code in a Console app, it should work (as long as you reference `System.Windows.Forms` and use the `[STAThread]` attribute. Does it work for you if you try that? – Matthew Watson Nov 04 '16 at 09:22
  • @All, Thanks for your support, I found my mistake and **update answer** – GSP Nov 04 '16 at 09:41

2 Answers2

-2
openFile.ShowDialog(); 

mean that will be opened modal window for current form(form where dialog box created)

mojo
  • 174
  • 1
  • 2
  • 9
  • This is not even close to an answer that will fix the problem of the OP – Peter B Nov 04 '16 at 09:16
  • http://stackoverflow.com/questions/478476/c-sharp-openfiledialog-non-modal-possible read this and try to understand. – mojo Nov 04 '16 at 09:35
-2

Try opening the dialogue from a separate thread, with a callback for when it returns. ShowDialogue is blocking the current thread, as you're waiting for the response from the dialogue to continue with your application behaviour - this means your application is going to hang until it's complete.

Callum Bradbury
  • 936
  • 7
  • 14
  • Because then it won't be blocking the application from continuing. – Callum Bradbury Nov 04 '16 at 09:15
  • Well, first of all I would expect the application to be blocked when a fileopendialog is shown, that's the normal behaviour. Second, if you don't want the dialog to block it,what about using `Show` instead of `ShowDialog`? – Pikoh Nov 04 '16 at 09:17
  • 1
    @Pikoh there is no Show for OpenFileDialog. However I concur with you that using a separate thread for this is a recipe for a disaster if not done well (and if possible at all). Some working code example could help – Steve Nov 04 '16 at 09:19
  • Steve you are right. There is no `Show` for OpenFileDialog (I've never tried to use it so I didn't realized it hasn`t). And that tells us something about how it should be used :) – Pikoh Nov 04 '16 at 09:22
  • @Steve what are the pitfalls it could fall into? I can't think of any and would welcome the reasoning why this isn't a good solution – Callum Bradbury Nov 04 '16 at 09:22
  • 1
    Well for starters, it simply won't work by default - you'd have to set the thread's apartment model to STA instead of MTA. – Matthew Watson Nov 04 '16 at 09:24
  • @CallumBradbury I have never done it because there is no real sense in making an OpenFileDialog pseudo non modal creating it in a separate thread. However running UI elements in a different thread should be handled very carefully. If you can provide a working example perhaps I could re-examine my preconcepts – Steve Nov 04 '16 at 09:26
  • @MatthewWatson Cool, cheers - I'll have to do some playing about with STA and MTA – Callum Bradbury Nov 04 '16 at 09:28
  • 1
    @CallumBradbury also look at [Hans Passant](http://stackoverflow.com/users/17034/hans-passant)'s answer here http://stackoverflow.com/questions/2834799/winforms-programming-modal-and-non-modal-forms-problem – Steve Nov 04 '16 at 09:30