Join 136,533 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,800 people online right now. Registration is fast and FREE... Join Now!
From the simple code below, how can I stop the Timer when the file is found? And how come my textBox inside the TimeEvent doesn't display? That txtBox I create under design view and given it a name as textBox. I try to disable the Timer, but it doesn't disable.
For what I really want is to stop the Timer after the file is found. Basically it will keep checkiing if the file has existed yet, if not then keep checking until the file exists. Although I'm not sure if that is an appropriate way to search for existing file.
CODE
public partial class Form1 : Form { public delegate void stopTime(object sender, stopTimeEventArg e);
private void button1_Click(object sender, EventArgs e) { textBox1.AppendText("searching for file..."); time = new System.Timers.Timer(); time.Enabled = true; time.Elapsed += new ElapsedEventHandler(TimerEvent); time.Interval = 2000;//every two seconds will raise an event. }
Did you get a Cross-thread operation not valid exception? I replicated your code, and it gave me an error under the TimerEvent function when i tried to access txtBox. If that is the case, changing the System.Timers.Timer object you are using to a System.Windows.Forms.Timer object should solve the problem. Basically, change your code to look like this
csharp
public delegate void stopTime(object sender, stopTimeEventArg e);
private void button1_Click(object sender, EventArgs e) { textBox1.AppendText("searching for file..."); time = new System.Windows.Forms.Timer(); time.Enabled = true; time.Tick += new EventHandler(time_Tick); time.Interval = 2000;//every two seconds will raise an event. }
void time_Tick(object sender, EventArgs e) { count++; if (File.Exists(textFile)) {
Recently I often get the Cross-thread operation, and I didn't know that Timer defined in system as well as windowform, however I don't have get any crossing thread operation based on my code above..
what is stopTimeEventArg? I use it and it gives me error. Mentioning I'm missing reference or namespace.
Use Timer.Start() to start the timer and Timer.Stop() to stop it. Setting the Enabled property only determines whether you can use the timer.
I'm sorry, but in my experience, they've both worked fine.
QUOTE(skyHigh @ 22 Aug, 2008 - 07:48 PM)
Recently I often get the Cross-thread operation, and I didn't know that Timer defined in system as well as windowform, however I don't have get any crossing thread operation based on my code above..
what is stopTimeEventArg? I use it and it gives me error. Mentioning I'm missing reference or namespace.
I have no idea what the stopTimerEventArg is. I too, had to delete that line entirely to make my application run when i tested it. I assumed it was something you were using for an unrelated task, so that's why i kept it in there. As far as i know, deleting that line entirely will resolve your error.
While it is true that the Enabled property will start and stop the timer, so I stand corrected, the Start and Stop method were designed for this purpose.
Thanks everyone for the replies. I tried to replace using Start() and Stop(), but still doesn't wait.
Here are the steps that I execute the program. Before I execute the program, I navigate to the currrent project folder to make sure there is no text file in there yet then I execute the program. After thrree or four seconds later I copy the text file and paste onto the currrent project folder and the messageBox says it found file, but it just doesn't stop.
private void button1_Click(object sender, EventArgs e) { textBox1.AppendText("searching for file...");
//Now usiing System.Timer. //time = new System.Timers.Timer(); //time.Start(); //time.Elapsed += new ElapsedEventHandler(TimerEvent); //time.Interval = 2000;//every two seconds will raise an event.
//Using Windows.Forms Timer. WindowFormTime = new System.Windows.Forms.Timer(); WindowFormTime.Enabled = true; WindowFormTime.Tick += new EventHandler(time_Tick); WindowFormTime.Interval = 2000;
}
//An Event for Using Windows.Forms Timer void time_Tick(object sender, EventArgs e) { count++; if (File.Exists(textFile)) {
For starters, you cannot alter a GUI element, in this case a TextBox, from outside the thread that created the element, which is the main thread the application runs in, except through a delegate. That's why you're getting the cross-thread exception. Here's a tutorial on Cross Thread Communication that discusses marshaling the call on to the main application's thread. It's written in C# but the same logic applies to all .Net languages.
For starters, you cannot alter a GUI element, in this case a TextBox, from outside the thread that created the element, which is the main thread the application runs in, except through a delegate. That's why you're getting the cross-thread exception. Here's a tutorial on Cross Thread Communication that discusses marshaling the call on to the main application's thread. It's written in C# but the same logic applies to all .Net languages.
That's strange because it doesn't complain about threading crossing error. It prints out the text phrase onto the textbox.
Can anyone please explain why this works because I just fix the code, but I don't understand why.
Based on the code above, I just switch the MessageBox.Show() coming after I set the timer enabled to False then it works. Now The message box no longer executed otherwise the message box displays over and over.
CODE
//An Event for Using Windows.Forms Timer void time_Tick(object sender, EventArgs e) { count++; if (File.Exists(textFile)) { WindowFormTime.Enabled = false; MessageBox.Show("File found! " + count + " " + textFile);