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.
- open file
- exit
When the user clicks 1. open file
, it will show (ShowDialog) a form with two buttons
- open file
- 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
- User can NOT interact with the form, it does NOT respond
- OpenFileDialog does NOT show
Could you please help explain reason why this problem occurs?
Update Answer
- The form which create icon in system tray is called from another thread.
- I set:
thread.SetApartmentState(ApartmentState.STA);
, OpenFileDialog will show.