避免竞争条件 - 并行编程

时间:2014-04-05 17:04:43

标签: c# wpf parallel-processing

我将此函数[ makeComputerMove ]与 Task.Factory.StartNew 语句一起使用,以避免在我的WPF应用程序中冻结UI。 我从 while循环调用此函数,我相信我有竞争条件。 我正在寻找一种等待功能补充的方法,然后继续前进。

private void makeComputerMove(PocGraph.Heuristic h, PocGraph.Player p)
{
    Node tree = null;
    TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext();
    CancellationToken cancelationToken = new CancellationToken();
    playLoadingStoryBoard(); 
    Task taskA = Task.Factory.StartNew(() => { tree = vm.buildMovesTree(h, p); }).
        ContinueWith(w =>
        {
            showWaterMarkForComputerMove(h.ToString(), tree.BestMove.Player.ToString(), tree.BestMove.Position);
            makeComputerMove(tree.BestMove.Position, tree.BestMove.Player);
            stopLoadingStoryBoard();

        },
        cancelationToken,
        TaskContinuationOptions.ExecuteSynchronously, scheduler);
}

While循环:

while (!gameOver() && !vm.Graph.hasWinner() && !vm.Graph.IsInfiniteLoop && isAIrunning)
                    {
                        vm.disableDelays = true;
                        //Black Move
                        makeComputerMove(blackHeuristic, PocGraph.Player.BLACK);

                        //Checking if someone won between turns
                        if (gameOver() || vm.Graph.hasWinner() || vm.Graph.IsInfiniteLoop)
                            break;

                        //Pause between Black -> Red
                        if (!cancelMessageBoxesAndWaiting)
                            vm.InteractivePauseInMilliSeconds(2000);

                        makeComputerMove(redHeuristic, PocGraph.Player.RED);

                        //Pause between Red -> Black
                        if (!cancelMessageBoxesAndWaiting)
                            vm.InteractivePauseInMilliSeconds(2000);
                    }
                    vm.disableDelays = false;

0 个答案:

没有答案
相关问题