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

977 views Asked by At

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.

2

There are 2 answers

3
Neeraj Kumar Gupta On BEST ANSWER

I am not sure but you can try

myMsgBox.Close() 

or

myMsgBox.Hide() 

it may help

0
Tarik On

Unless you have a long running or slow operation, there is no reason why you should complicate your life with a separate thread. Just show your dialog form in modal mode and proceed when the user has input the required data.