Timers are repetitive; i.e. they continue to tick until explicitly stopped.
If you only wish to read/send each line in turn then you could just replace the while with an if in the timer event.
private void timer1_Tick(object sender, EventArgs e)
{
if ((line = file.ReadLine()) != null)
{
textToSend ="{Enter}" + line + "{Enter}";
textBox1.AppendText(textToSend);
SendKeys.Send(textToSend);
}
}
However, I would recommend that you read the file in to an array of strings and close it.
Then, using an index to keep track, send each line using the timer.
That way you do not keep the file open for long.
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
Doing a for loop with a delay on the button will lock up the UI thread until all lines are processed.
The timer idea is correct.
Something like this:
private int nextText;
public void button1_Click(object sender, EventArgs e)
{
nextText = 0;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (nextText < textToSendArray.Length)
{
textBox1.AppendText(textToSendArray[nextText++] + "\n");
}
else
{
timer1.Stop();
}
}
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187