我有一个应用程序可以将固件编程到电路板上。在应用程序中,您可以编写单个板或托盘。编程托盘时,一次只能加载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 <14)并且它导致它运行正常,但它没有停止。
任何帮助都会受到大力赞赏!
提前谢谢你, 露
答案 0 :(得分:2)
正如代码所示,您的for
循环内容永远不会被执行。 for
循环中的条件是 continue 条件。由于i
初始化为0
,因此永远不会满足条件i > 14
。因此,结果是无限外while
循环。
i < 14
的第一次“意外”是正确的。但是循环没有停止,因为你的内部while
循环永远不会完成:
while (process.HasExited == false) //while open
{
process.StandardInput.WriteLine(); //send enter key
}
首先,请不要将bool
与true
或false
进行比较。一个简单的while (!process.HasExited)
就足够了。
其次,您必须refresh您的流程实例才能正确更新HasExited
属性:
while (!process.HasExited) //while open
{
process.StandardInput.WriteLine(); //send enter key
process.Refresh(); // update the process's properties!
}
您也可以考虑在该循环中添加Thread.Sleep(...)
。
答案 1 :(得分:1)
答案很简单:
while (numberOfBoards > counter) {
for (int i = 0; i > 14; i = i + 1) {
在上面的代码中,永远不会执行for循环,因为我将始终小于14。
因为这样,计数器永远不会增加,而且,永远不会完成。
但除此之外,你对循环的方法是错误的。以下示例(完全测试程序)是您应该做的事情:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int counter = 0;
int i = 0;
int numberOfBoards = 35;
for (; numberOfBoards > counter; i++, counter++)
{
Console.WriteLine("Counter {0}/i {1}", counter, i);
//call your thread here.
//make sure that he exists.
//use somekind of timeout to finish
//alert the user in case of failure but move to the next anyway to avoid an infinite looping.
if (i == 13) i = 0;
}
Console.WriteLine("Press any key");
Console.ReadKey();
}
}
}