C#在方法执行期间动态分配和更新表单
本文关键字:更新 表单 动态分配 方法 执行期 | 更新日期: 2023-09-27 18:19:57
在方法的循环仍在工作期间,我如何动态地分配值并使它们在表单上更新?在我的程序中,当启动时,它会挂起,直到它完成(找到数独的解决方案),然后填充文本框。以下是主要方法:
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;
}
}
一个选项是后台工作者。请在此处查看有关设置后台工作者的更多信息。:
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
}
}
}