Click here to Skip to main content
15,908,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am a beginner in c #. I want to make a function that captures
the contents of a text file by checking in a textBox
text content every 2 minutes.when the value entered in the textBox is equal to 1 an alarm will be triggered. I managed to capture content of the text file in the texteBox. when it equal to 1 is an alarm declenche.My problem is how to control this textBox every 2 minutes.here my code:
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace streamReader
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Creating a new stream-reader and opening the file.
System.IO.StreamReader StreamReader1 = new System.IO.StreamReader(@"..//..//déclenche priorité//priorité.txt");

//Reading Line from file
string newLine;
while ((newLine = StreamReader1.ReadLine()) != null)


//Writing the read line to textBox
textBox1.AppendText(newLine);

//Close the file
StreamReader1.Close();
}






private void textBox1_TextChanged(object sender, EventArgs e)
{


if (textBox1.Text == "1")
{
SoundPlayer player = new SoundPlayer();
string chemin = "..//..//Sequences//alarme.wav";
player.SoundLocation = chemin;
player.Play();
}

}


}
}

thank you for helping me
Posted
Updated 9-May-11 3:04am
v2

See this CP article about Timer. Pre-beginner's guide to using a timer[^] and All about .NET Timers - A Comparison[^]

The basic is. Create timer and setup. Tell interval, the method to be called and then start the timer.


Drag and drop a System.Windows.Forms.Timer[^] in the designer and setup the timer. Interval and Tick event handler.
Then just start the timer with timer1.Start();
 
Share this answer
 
v2
Comments
sawass 9-May-11 10:38am    
Thank you for your reply,
but what you can tell me again how to use The timer function in my function.
sawass 9-May-11 10:44am    
Thank you for your reply,
but what you can tell me again how to use The timer function in my function.
Kim Togo 9-May-11 14:59pm    
See updated answer. Hope it can help you.
Sergey Alexandrovich Kryukov 9-May-11 22:28pm    
My 5.
Please see my answer where I explain why thread should be used, not timer.
--SA
This problem is best solved using threads, not timers. It's actually much easier and gives more robust results. Never use a timer where a thread can be used.

Please see my past answer where this is explained:
Timer Threading in C#[^].
Please see a collection of my past answers on threads here:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].


—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900