我有一個應用程序將固件編程到電路板。在應用程序中,您可以編寫單個板或托盤。編程托盤時,一次只能裝載14個托盤。 用戶可能想編寫30個板,所以我想讓程序對14個板編程,然後告訴用戶他們需要重新加載一個托盤。 目前我只有一塊板子可以練習,所以我剛剛重新編程假裝它的一個托盤。 我試圖用循環來解決這個問題,但是當我按下開始按鈕時,它全部凍結並停止響應。for循環不斷讓我的程序崩潰c#
下面是我的代碼:
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循環中,我無意中把(我< 14),它導致它運行良好,但它並沒有停止。
任何幫助將大規模讚賞!
謝謝你在前進, 露西
'for'循環中的條件是_continue_條件。因此,只有當'i> 14'從0開始初始化時纔會進入循環。所以你用'我'14'的「事故」實際上是正確的。乍看之下,爲什麼它沒有停止呢?請花時間編輯您的問題並設置格式以提高可讀性。 –
for循環中的任何代碼都不會執行。因此你有一個無限的while循環。 – adv12
好的,謝謝!我現在試試吧 – lucycopp