【发布时间】:2016-08-23 13:43:06
【问题描述】:
我有一个将固件编程到电路板的应用程序。在应用程序中,您可以对单个板或托盘进行编程。对托盘进行编程时,一次只能装载 14 个。 用户可能想要对 30 个板进行编程,所以我希望程序对 14 个板进行编程,然后告诉用户他们需要重新加载托盘。 目前我只有一个板子可以练习,所以我只是在重新编程同一个板子,假装它是一个托盘。 我尝试使用循环来解决这个问题,但是当我按下开始按钮时,它全部冻结并停止响应。
以下是我的代码:
private void setFirmwareMultiple()
{
clearTicksandCrosses();
string firmwareLocation = Firmware(productComboBox.Text); //get the firmware location
string STPath = @"C:\Users\Falconex\Documents\FalconexTest\FalconexTest\ST-LINK Utility\ST-LINK_CLI.exe"; //file location
string result; //set string
string result2; //set string
int counter = 0;
int numberOfBoards = int.Parse(numberOfBoardsTextBox.Text);
while (numberOfBoards > counter) {
for (int i = 0; i > 14; i = i + 1) {
ProcessStartInfo start = new ProcessStartInfo(); //new process start info
start.FileName = STPath; //set file name
start.Arguments = "-C -ME -p " + firmwareLocation + " -v -Run"; //set arguments
start.UseShellExecute = false; //set shell execute (need this to redirect output)
start.RedirectStandardOutput = true; //redirect output
start.RedirectStandardInput = true; //redirect input
start.WindowStyle = ProcessWindowStyle.Hidden; //hide window
start.CreateNoWindow = true; //create no window
string picNumber = i.ToString();
using (Process process = Process.Start(start)) //create process
{
programmingTextBlock.Text = "Board Programming...";
System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
new Action(delegate { }));
try
{
while (process.HasExited == false) //while open
{
process.StandardInput.WriteLine(); //send enter key
}
using (StreamReader reader = process.StandardOutput) //create stream reader
{
result = reader.ReadToEnd(); //read till end of process
File.WriteAllText("File.txt", result); //write to file
}
saveReport();
}
catch { } //so doesn't blow up
finally
{
int code = process.ExitCode; //get exit code
codee = code.ToString(); //set code to string
File.WriteAllText("Code.txt", codee); //save code
if (code == 0)
{
tick1.Visibility = Visibility.Visible;
counter = counter + 1;
}
else
{
cross1.Visibility = Visibility.Visible;
}
programmingTextBlock.Text = "";
}
}
System.Windows.MessageBox.Show("Load new boards");
}
}
}
我已将用户想要的板总数放入 for 循环中。
我认为这可能与 for 循环有关。因为一开始,在 for 循环中,我不小心放了 (i
任何帮助将不胜感激!
提前谢谢你, 露西
【问题讨论】:
-
for循环中的条件是 continue 条件。所以只有i > 14才会进入循环,因为它是用0初始化的,所以它永远不会发生。所以你与i <14的“意外”实际上是正确的。乍一看无法说出为什么它没有停止。请花时间编辑您的问题并格式化代码以提高可读性。 -
for 循环中的任何代码都不会被执行。因此你有一个无限的while循环。
-
好的,谢谢!我马上试试
-
那么我会将 i 设置为什么?我很困惑
-
您拥有
while (numberOfBoards > counter) { for (int i = 0; i > 14; i = i + 1) {,因此对于每次迭代,您将运行它 14 次,并且仅当返回码为零时计数器才会增加。 numberOfBoards 是什么值,并使用调试器单步执行,看看计数器是否在增加。