//delegate void CancellableAction(Func<bool> canceler); //private void Solve_Click(object sender, RoutedEventArgs e) //{ // bool cancel = false; // CancellableAction solve = _board.Solve; // var result = solve.BeginInvoke(() => cancel, null, null); // CancelWindow c = new CancelWindow(() => result.IsCompleted, 100); // c.Owner = this; // if (c.ShowDialog() == false && !result.IsCompleted) // { // cancel = true; // result.AsyncWaitHandle.WaitOne(); // } //} private void Solve_Click(object sender, RoutedEventArgs e) { var board = new SokobanLogic.SimpleBoard(); //var board = _board; board.Init(_boardInfo); SokobanLogic.MoveType mt = SokobanLogic.MoveType.Start; CancelWindow c = new CancelWindow(() => { if (board.IsSolved() || !board.MoveNext(ref mt)) { return(true); } mt = board.IsInDeadLock() ? SokobanLogic.MoveType.Stop : SokobanLogic.MoveType.Start; return(false); }, 0); c.Owner = this; if (c.ShowDialog() == false || board.IsSolved()) { MessageBox.Show(board.Solution(), "Solution"); } }
//delegate void CancellableAction(Func<bool> canceler); //private void Solve_Click(object sender, RoutedEventArgs e) //{ // bool cancel = false; // CancellableAction solve = _board.Solve; // var result = solve.BeginInvoke(() => cancel, null, null); // CancelWindow c = new CancelWindow(() => result.IsCompleted, 100); // c.Owner = this; // if (c.ShowDialog() == false && !result.IsCompleted) // { // cancel = true; // result.AsyncWaitHandle.WaitOne(); // } //} private void Solve_Click(object sender, RoutedEventArgs e) { var board = new SokobanLogic.SimpleBoard(); //var board = _board; board.Init(_boardInfo); SokobanLogic.MoveType mt = SokobanLogic.MoveType.Start; CancelWindow c = new CancelWindow(() => { if (board.IsSolved() || !board.MoveNext(ref mt)) return true; mt = board.IsInDeadLock() ? SokobanLogic.MoveType.Stop : SokobanLogic.MoveType.Start; return false; }, 0); c.Owner = this; if (c.ShowDialog() == false || board.IsSolved()) MessageBox.Show(board.Solution(), "Solution"); }