0
StreamReader file = new StreamReader(@"C:\Users\User\Documents\Files.txt");

while ((line = file.ReadLine()) != null)
{
    richTextBox1.Text += Environment.NewLine + "Copying: " + line;
    counter++;
}

I have this Code to read an textfile with multiple paths in it. What i want to do is to post them in a textbox which I got so far, but my question is can i do a 1sec delay between each line the streamreader is going to post?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Ayres
  • 5
  • 3

3 Answers3

1

Something of this sort:

System.Threading.ThreadPool.QueueUserWorkItem((obj) =>
{
    StreamReader file = new StreamReader(@"C:\Users\User\Documents\Files.txt");
    string line;
    int counter = 0;
    while ((line = file.ReadLine()) != null)
    {
        this.Invoke((Action)(() =>
            {
                richTextBox1.Text += Environment.NewLine + "Copying: " + line;
            }));
        System.Threading.Thread.Sleep(1000);
        counter++;
    }
});

Alternatively as suggested in comments above, a BackgroundWorker could also be used.

Documentation

theB
  • 6,450
  • 1
  • 28
  • 38
0

1) Use System.Windows.Forms.Timer

Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in Windows Forms applications and must be used in a window.

2) Read all lines in Queue.

Queue<string> lines = new Queue<string>( File.ReadAllLines( @"Path" ) );

3) Use timer event to read per line in List.

private void Timer_Tick( object sender, EventArgs e )
    {
        if ( lines.Count > 0 )
        {
            richTextBox1.Text += Environment.NewLine + "Copying: " + lines.Dequeue();
            counter++;
        }
    }

Check for empty Queue.Dequeue Method ( lines.Count > 0 or another, stop timer when it's become true)

MrDywar
  • 216
  • 2
  • 7
-1

You could use the System.Threading class to call: Thread.Sleep(1000); once per loop iteration.

More information on the System.Threading class at MSDN

EDIT:

As mentioned below, using Thread.Sleep causes the UI to lock up during the Sleep method. As an alternative, you may like to try the BackgroundWorker class.

The following snippet assumes you want to trigger your code above from a button click (it was unclear from the question whether this is actually the case or not).

private void startAsyncButton_Click(object sender, EventArgs e)
{
   if(backgroundWorkerA.IsBusy != true)
   {
      //start the Async Thread
      backgroundWorkerA.RunWorkerAsync();
   }
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
   StreamReader file = new StreamReader(@"C:\Users\User\Documents\Files.txt");

   while ((line = file.ReadLine()) != null)
   {
       richTextBox1.Text += Environment.NewLine + "Copying: " + line;
       counter++;
       Thread.Sleep(1000);
   }
}

Here you are simply creating a worker thread to do the (relatively) time-consuming task without tying up your GUI.

umphish
  • 75
  • 1
  • 9
  • As Alexei points out, this will cause the UI to freeze for a second at a time. If you do not want such functionality, I suggest investigating the BackgroundWorker class. – umphish Sep 08 '15 at 20:11
  • You *should not* use `Thread.Sleep` on UI thread. Doing so on non-UI thread requires much more code than shown so far in this post. – Alexei Levenkov Sep 08 '15 at 20:13
  • Yeah just found out that my ui dies – Ayres Sep 08 '15 at 20:13
  • @Ayres see above for edited answer including BackgroundWorker information. – umphish Sep 08 '15 at 20:31