2016-08-23 160 views
-4

我有一個應用程序將固件編程到電路板。在應用程序中,您可以編寫單個板或托盤。編程托盤時,一次只能裝載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),它導致它運行良好,但它並沒有停止。

任何幫助將大規模讚賞!

謝謝你在前進, 露西

+1

'for'循環中的條件是_continue_條件。因此,只有當'i> 14'從0開始初始化時纔會進入循環。所以你用'我'14'的「事故」實際上是正確的。乍看之下,爲什麼它沒有停止呢?請花時間編輯您的問題並設置格式以提高可讀性。 –

+2

for循環中的任何代碼都不會執行。因此你有一個無限的while循環。 – adv12

+0

好的,謝謝!我現在試試吧 – lucycopp

回答

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(); 
     } 
    } 
} 
+0

它仍然不工作 - 只會凍結 – lucycopp

+0

@lucycopp,因爲您正在增加「最後」塊內的「計數器」,但只有在發生異常時纔會執行... – Jauch

+0

這不是真的,整體意義上的'finally'塊將在各種情況下執行。凍結的原因是內部while循環。看我的回答 –

2

正如代碼現在站立,你for循環的內容永遠不會被執行。在for循環中的條件是繼續條件。由於i使用0初始化,所以從未滿足條件i > 14。所以結果是無限的while循環。

你的第一個「事故」與i < 14是正確的。但隨後的循環沒有停止,因爲你內心while循環永遠不會結束:

while (process.HasExited == false) //while open 
{ 
    process.StandardInput.WriteLine(); //send enter key 
} 

首先,請不要比較一booltruefalse。一個簡單的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(...)

+0

仍然保持凍結:( – lucycopp

+0

@lucycopp您是否驗證過程_does_退出? –

+0

當我通過調試器時,它甚至不會進入for循環,它會跳到i <14,然後跳過所有內容 – lucycopp