2016-07-22 98 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