2016-07-22 251 views
0

我想讀取單行並將其放入文本框中,每1秒。我管理此代碼:定時器和while循環

private void button1_Click(object sender, EventArgs e) 
{ 
    while ((line = file.ReadLine()) != null) 
    { 
     timer.Start(); 
    } 
} 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    textBox1.text += line + "\r\n"; 
} 

但是line不可訪問。我也試過這樣的:

private void button1_Click(object sender, EventArgs e) 
{ 
    timer.Start(); 
} 


private void timer1_Tick(object sender, EventArgs e) 
{ 

    while ((line = file.ReadLine()) != null) 
    { 
     textBox1.Text += line + "\r\n"; 
    } 

} 

但它忽略了定時器間隔。更重要的是,我不知道如何在兩個示例中停止計時器。你能給我一個提示,我可以用這個做什麼?

編輯

任何想法,爲什麼這個代碼工作好與if但與while

private void timer1_Tick(object sender, EventArgs e) 
     { 

      while ((line1 = file1.ReadLine()) != null) 
      { 


       while ((line2 = file2.ReadLine()) != null) 
       { 

        try 
        { 
          //some code 
        } 

        catch 
        { 

          //some code 
        } 

        finally 
        { 
          //some code 

        } 

       } 



      } 

       timer1.Stop(); 


     } 

我想每一行從file2與每一行從file1結合起來。

+0

第一個例子會做奇怪的事情,第二個例子是非常不清楚,因爲我們不知道,你的計時器是如何初始化 – lokusking

+1

提示:使它成爲全局的,並在timer1_Tick()中,讀取下一行,顯示它並重新啓動計時器。 –

+0

第一塊代碼表示「對於文件中的每一行,啓動計時器」。我認爲這可能是你的問題的一部分。如果你在while塊後加上'timer.Start',那麼你可能會進一步。我也不知道你的意思是「但是'行'不可訪問」。你的意思是你沒有看到文本框中的文字,但你期待?您也不會將任何這些行添加到該循環內的文本字段中,因此整個文件(除最後一行「空行」之外)將在發生任何事情之前被讀取和丟棄。 –

回答

2

此代碼將從給定文件(當前爲c:\ myfile \ test.xml)中取出一行並將其讀入數組,然後啓動計時器。計時器一旦啓動,它將確定您的數據是否已滿足。如果仍有數據,則將其添加到文本框中。如果沒有數據,計時器將停止。您可以再次按下該按鈕重新開始該過程。

//holds each line of the file contents 
    string[] lines = null; 
    //sets the current line number that you are at in the lines array 
    int curline = 0; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     //reads all lines of files and starts the timer 
     lines = File.ReadAllLines(@"C:\myfile\test.xml"); 
     curline = 0; 
     timer1.Start(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     //if not end of data then insert on another line of the textbox 
     if (curline < lines.Length) 
     { 
      textBox1.Text += lines[curline] + "\r\n"; 
      curline++; 
     } 
     else 
     { 
      //else stop the timer 
      timer1.Stop(); 
     } 
    } 

編碼快樂, 傑森

+0

+1,用於正確確定變量的範圍。我建議在隊列中使用隊列,而不是字符串數組,並且每次使用lines.Dequeue()在timer1_Tick上拉線,而不是使用curline字段。不過,你的方法是可行的。 –

+0

@audiophonic您應該將其作爲新問題發佈 – MickyD

1

在第二個例子中的代碼更改爲以下

private void timer1_Tick(object sender, EventArgs e) { 
    if((line = file.ReadLine()) != null) 
    { textBox1.Text += line + "\r\n"; 
    } 
    else 
    { 
    //Stop Timer here 
    } 
} 
+1

唯一的問題是,在完成處理之後,您已將該文件的'文件'對象的鎖定保留在打開狀態。雖然這可能比這個練習更令人擔憂。 –

0

我最好的選擇:(假定myFilePath包含路徑到您正在使用的文件)

 private Queue<string> _lines; 

    private void button1_Click(object sender, EventArgs e) 
    { 
     _lines = new Queue<string>(System.IO.File.ReadAllLines(myFilePath)); 
     textBox1.Text = string.Empty; 
     timer1.Start(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     if (_lines.Any()) 
      textBox1.Text += _lines.Dequeue() + Environment.NewLine; 
     else 
      timer1.Stop(); 
    } 
1

這是解決方案:

正如在評論中提到的那樣,在開始和後臺開始一切。首先閱讀你的文件,並準備你的線的容器。然後,啓動一個計時器。現在,每當timer1.Ticks時,我們都會從第一行開始顯示容器中的一行。我們一直在做這個過程。

小心你的文件路徑。

MyFile的:"C:\Users\Public\TestFolder\Test.txt"

一個

b

Ç

d

Ë

˚F

代碼可以編譯:

using System; 
using System.Linq; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class ExampleProgram : Form 
    { 
     // global variables 
     string[] MyLines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\Test.txt"); // contain all lines 
     int MyCurrentLine = 0; 

     public ExampleProgram() 
     { 
      InitializeComponent(); 
      StartProgram(); // start the program 
     } 

     private void StartProgram() 
     { 
      timer1.Interval = 1000; // set interval in milliseconds 
      timer1.Start(); // start timer 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      //timer1.Stop(); // first thing first - Edit: Looks like you did not need this. 
      textBox1.Text = MyLines[MyCurrentLine % MyLines.Count()]; // display next line 
      ++MyCurrentLine; // increment counter 
      //timer1.Start(); // restart - Edit: And also won't need this. Less code.. Nice ! 
     } 
    } 
} 

結果:

enter image description here

+0

爲什麼要在'timer1_Tick'中停止/啓動計時器?這不是好像回調需要很長時間 – MickyD

+0

@MickyD我不確定,但教我這個教授告訴我這麼做。也許我應該刪除它然後:)謝謝指出, –

+0

所有的好。好的動畫太順道了,更多的SO答案應該這樣:) – MickyD