Silverlight Confirm Dialog to Pause Thread

Posted by AlishahNovin on Stack Overflow See other posts from Stack Overflow or by AlishahNovin
Published on 2010-01-21T22:37:59Z Indexed on 2010/04/21 0:53 UTC
Read the original article Hit count: 1269

I'm trying to do a confirmation dialog using Silverlight's ChildWindow object.

Ideally, I'd like it to work like MessageBox.Show(), where the entire application halts until an input is received from the user.

For example:

for(int i=0;i<5;i++) {
  if (i==3 && MessageBox.Show("Exit early?", "Iterator", MessageBoxButton.OKCancel) == MessageBoxResult.OK) {
    break;
  }
}

Would stop the iteration at 3 if the user hits OK...

However, if I were to do something along the lines:

ChildWindow confirm = new ChildWindow();
confirm.Title = "Iterator";
confirm.HasCloseButton = false;
Grid container = new Grid();

Button closeBtn = new Button();
closeBtn.Content = "Exit early";
closeBtn.Click += delegate { confirm.DialogResult = true; confirm.Close(); };
container.Children.Add(closeBtn);

Button continueBtn = new Button();
continueBtn.Content = "Continue!";
continueBtn.Click += delegate { confirm.DialogResult = false; confirm.Close(); };
container.Children.Add(continueBtn);

confirm.Content = container;

for(int i=0;i<5;i++) {
  if (i==3) {
    confirm.Show();
    if (confirm.DialogResult.HasResult && (bool)confirm.DialogResult) {
      break;
    }
  }
}

This clearly would not work, as the thread isn't halted... confirm.DialogResult.HasResult would be false, and the loop would continue past 3.

I'm just wondering, how I could go about this properly. Silverlight is single-threaded, so I can't just put the thread to sleep and then wake it up when I'm ready, so I'm just wondering if there's anything else that people could recommend?

I've considered reversing the logic - ie, passing the actions I want to occur to the Yes/No events, but in my specific case this wouldn't quite work.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about confirm