0

如何在方法的循环仍在工作期间动态地分配值并使它们在表单上更新?我的程序中发生的情况是,启动时它会挂起,直到完成(找到数独的解决方案),然后填充文本框。下面是主要方法:

            bool SolveSudoku()
    {
        if (!FindUnassignedLocation()) return true;
        for (int num = 1; num <= 9; num++)
        {
            if (NoConflicts(emptyRow, emptyCol, num))
            {
                Grid[emptyRow, emptyCol].Text = num.ToString();
                Grid[emptyRow, emptyCol].BackColor = Color.White;
                Grid[emptyRow, emptyCol].ForeColor = Color.Black;
                if (SolveSudoku()) return true;
                Grid[emptyRow, emptyCol].Text = "";   
            }
        }
        bool checkIfFirstBackTrack = true;
        do {
            if (checkIfFirstBackTrack)
            {
                backtrackCounter++;
                checkIfFirstBackTrack = false;
            }
            if (emptyCol == 0 && emptyRow > 0)
            {
                emptyCol = 8;
                emptyRow--;
            }
            else if (emptyCol > 0)
            {
                emptyCol--;
            }
        }
        while(Preset[emptyRow, emptyCol] != "");

        return false;
    }
    }
4

1 回答 1

1

一种选择是后台工作人员在此处查看有关设置后台工作人员的更多信息。

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;

    for (int i = 1; i <= 10; i++)
    {
        if (worker.CancellationPending == true)
        {
            e.Cancel = true;
            break;
        }
        else
        {
            //// your code here
        }
    }
}
于 2013-06-26T18:51:23.300 回答