Search code examples
c#wpfuser-inputmessagebox

C# Do While Message Box is active


I am working on a project in C# that requires user input from a pop-up message box.

I am also trying to have my code perform a series of tasks while the message box is active.

My goal is to have the MessageBox describe the tasks being performed and ask the user to observe and verify that they are being carried out. I want these tasks to continuously be performed until the user clicks on a response in the MessageBox.

To create my message box, I am using:

MessageBox.Show(string <message>, string <caption>, MessageBoxButton.YesNo)

And the basic structure of what I am trying to do is as follows:

var userInput = MessageBox.Show("Are tasks A, B, C running?", "winCaption", MessageBoxButton.YesNo);
while (// <No response to MessageBox>)
{
    // Task A
    // Task B
    // Task C
    if (userInput == MessageBoxResult.Yes)
    {
        // PASS
        // exit while loop
    }
    else
    {
        // FAIL
        // exit while loop
    }
}

I have found that when the MessageBox.Show() occurs & the pop-up window appears, the code seems to hang at that line until the user response is detected.

Is it possible to make this work? If not, are there any alternatives?

Thank you


Solution

  • How about calling MessageBox on separate Thread?

    var action = new Action(() =>
    {
          var userInput = MessageBox.Show("Are tasks A, B, C running?", "winCaption", MessageBoxButtons.YesNo);
          if (userInput == DialogResult.Yes)
          {
               // PASS
          }
          else
          {
                // FAIL
          }
    });
    new Thread(new ThreadStart(action)).Start();
    

    enter image description here