Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight Confirm Dialog to Pause Thread

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!

like image 869
AlishahNovin Avatar asked Jan 21 '10 22:01

AlishahNovin


1 Answers

I don't think you'll be able to block your code in a message loop the way you can with WinForms' ShowDialog.

However, you can misuse iterators to achieve the same effect:

interface IAction { void Execute(Action callback); }

public static void ExecAction(IEnumerator<IAction> enumerator) {
    if (enumerator.MoveNext())
        enumerator.Current.Execute(() => ExecAction(enumerator));
}

class DialogAction : ChildWindow, IAction {
    void IAction.Execute(Action callback) {
       //Show the window, then call callback when it's closed
    }
}

IEnumerator<IAction> YourMethod() { 
    ...
    var confirm = new DialogAction();
    yield return confirm;
    if (confirm.DialogResult.HasResult && (bool)confirm.DialogResult)
        yield break;
    ...
}

To use this system, you would write ExecAction(YourMethod());. Note that this would be a semi-blocking call, and that I haven't tested this at all.

C#5's new async features work exactly the same way (in fact, the initial versions of the async compiler code were heavily based on the existing iterator implementation), but with nicer syntactic support.

like image 90
SLaks Avatar answered Sep 28 '22 09:09

SLaks