Hello, first this is not a homework assignment so please respond whenever you have time as a speedy solution is not exactly needed. I have been learning C# recently from a microsoft book and had an idea of a program i want to make. Basically i want to be able to read a text file, then after reading it line by line send the lines somewhere else WITH a time delay. Here is where i have stalled. I do not know how to send the text, then wait lets say 20 sec, then send the next line and so on.So what i looking is for for code snippets or something which deals with this. Here is what i have tried so far using timer.

private void timer1_Tick(object sender, EventArgs e)
        {

          
                while ((line = file.ReadLine()) != null)
                { 
                    textToSend ="{Enter}" + line + "{Enter}";
                    textBox1.AppendText(textToSend);
                    SendKeys.Send(textToSend);
                }
                     
               
            

        }

basically there is my timer method which reads line from a file i have already opened. Makes a new string with {Enter} start and end. Which means that it will send the string in the window if we have focus.I plan to add a hotkey for that later. What i want to do now in this while loop is wait 30 sec after each line is read and then send the next line.
In another method which i call i have these two lines which i think will do what i want them to do.

public void start()
        {
            
            timer1.Interval = 10000;
            timer1.Start();       
        }

Recommended Answers

All 4 Replies

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.

Ok i went that route and made an array which is initiated after i read all the lines and get the line count. Now what i want to do is have a loop, and every 20 seconds, i want it to send the next keys. First im trying to debugg this using a textbox but im not sure how to go about it. What i have is an array called textToSendArray. What i want to do is something close to this.

public void button1_Click(objext sender, EventArgs e)
{
     for (int i = textToSendArray.Length; k < i;k++)
            {
                
                textBox1.AppendText(textToSendArray[k] + "\n");
                //here is where i want it to wait 20 seconds, before going
                //through the loop again and writing the next line in the array
                //20 seconds later after it wrote the first line
                // something similar to Thread.Sleep(20000);


            }
}

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

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:

You are right. I had this going on and while it did seem to work as it was displaying the lines in the test textbox delayed, it was locking the UI every other second or so.

int k = 0;
            for (int i = textToSendArray.Length; k < i; k++)
            {
                
                textBox1.AppendText(textToSendArray[k]+"\n");
                System.Threading.Thread.Sleep(2000);
                Application.DoEvents();

            }

I will try the timer one in a bit and post results.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.