Search code examples
c#windowmodal-dialogshowdialog

Code after showDialog is not execute when the modal window is closed by the other thread -- C#


In my program, I need to pop up the modal window prompt user to do some operations, the modal windows close automatically once user finish the operation.

in one thread, the code will be blocked at ShowDialog() and the code after it cannot be executed. the Main source code like this:

MyMessageBox myMsgBox = null

private void UserInputRead()
{
  myMsgBox = new MyMessageBox(); //customerized messageBox extends from Form
  //some operation
  Thread t1 = new Thread(UserInputListener);
  t1.start();
  myMsgBox.ShowDialog();
  Console.WriteLine("...after showDialog()");
}

in the other thread, it monitor the user input, if meet requirement then dispose the modal dialog.

private void UserInputListerner()
{
  //monitor user input 
  if(xxx) //user input meet the requirement
  {
     myMsgBox.Dispose()
  }
}

when the user input meet requirement, the modal dialog has been disposed, but the code after ShowDialog() still can not be executed, but when I tried to use the Timer or KeyEventHandler to dispose this modal dialog, the code after ShowDialog() can be executed.

The requirement is to automatically close the modal window when the user finish some operations and some other handling after close the modal window.

Any ideal how to resolved it and implement this requirement? Thanks a lot in advance.


Solution

  • I am not sure but you can try

    myMsgBox.Close() 
    
    or
    
    myMsgBox.Hide() 
    

    it may help