Click to See Complete Forum and Search --> : Timer behavoir?
mase
August 25th, 2008, 01:07 PM
Below is my simple code and I'm just curious why the statement " richTextBox1.AppendText("\nEnd of Contrustor");" is executed before the time starts. I would like it to execute after the timer ends, can anyone help me?
thanks.
public Form1()
{
InitializeComponent();
call_searchFile();
richTextBox1.AppendText("\nEnd of Contrustor");
}
private void call_searchFile()
{
richTextBox1.AppendText("Calling to search_file()");
search_File();
if(fileFound== true)
richTextBox1.AppendText("\nFinish search file, do next task");
}
private void search_File()
{
//Using Windows.Forms Timer.
WindowFormTime = new System.Windows.Forms.Timer();
WindowFormTime.Enabled = true;
WindowFormTime.Tick += new EventHandler(time_Tick);
WindowFormTime.Interval = 2000;
}
void time_Tick(object sender, EventArgs e)
{
count++;
if (File.Exists(textFile))
{
fileFound = true;
WindowFormTime.Enabled = false;
richTextBox1.AppendText("\nWindow Timer: File found! " + count + " " + textFile);
WindowFormTime.Stop();
}
else
{
richTextBox1.AppendText("\nFile Not found! " + count);
}
}
darwen
August 25th, 2008, 01:31 PM
You're using a windows forms timer. That means it goes through the application's message loop.
If you're in any code in the form the timer tick will not fire until you exit it. It's just the way it works. The timer tick is NOT multithreaded therefore must be run on the same thread that it is created on. If you're executing code already on this thread it has to wait until you've finished (this is the best explanation I can come up with without going into what the application message loop is).
You might want to investigate using threads to do the file search.
And if you do read up on the Control.InvokeRequired property (google for it). Otherwise you'll hit the same pitfalls with addressing controls on forms from other threads that everyone else does...
Darwen.
mase
August 25th, 2008, 01:47 PM
darwen,
Thanks for your quick respond and now I understand Window Form Timer What if I use System Timer, will that be the same?
If System Timer doesn't work neither, I guess threading the only suitable way to check if file has create yet.
I'm going to google Control.InovkeRequired.
darwen
August 25th, 2008, 02:52 PM
There's System.Threading.Timer, but this is a multithreaded timer so you'll still encounter the same issues with this as if you created a separate thread.
Darwen.
mase
August 25th, 2008, 03:02 PM
Sorry to ask too much question, but can you explain to me why System.Timer doesn't work because you said it's mutlithread so I assume it won't use the same thread the UI application right?
very appreciate of your explaination.
thanks.
darwen
August 25th, 2008, 03:06 PM
There's no such thing as System.Timer.
System.Threading.Timer will work : but I'm not going to go into the pitfalls (again, 50th time on this forum). If you google Control.InvokeRequired this'll give you pages about accessing UI controls from a different thread.
Darwen.
mase
August 25th, 2008, 03:29 PM
Then I guess I'm going to search in the forum about the Pitfalls that you mentioned. Hope I will be able to find some.
thanks.
mase
August 25th, 2008, 03:45 PM
Wait you said there is no such System.Timers? but I have been using it unless they are the same to windows.Forms
private System.Timers.Timer SystemTimer;
private System.Windows.Forms.Timer WindowFormTime;
As for this one, System.Threading.Timer, it's very new to me. And I always thinks this -->System.Timers.Timer SystemTimer<-- as system timer
Nevermind please disregard my this post because I just found a link explaining the Time class from this forum.
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
jasonli
August 26th, 2008, 08:39 AM
Why don't you put that line at the end of timer event? Timer is also kind of thread, program will go thru all lines in calling function and then run the timer event.
mase
August 26th, 2008, 03:33 PM
Why don't you put that line at the end of timer event? Timer is also kind of thread, program will go thru all lines in calling function and then run the timer event.
Yes I can just do that, but the thing is that line is just an example to see which block of code executes first because I want to continue my other tasks after the timer event ends. It's going to be complicated for me if I put other block of code at the end of timer event because my project is not implemented around the time.
thanks for your input.
mase
August 26th, 2008, 03:39 PM
Is it ok to instantiate System.Threading.Timer in the constructor?
thanks.
jasonli
August 26th, 2008, 03:45 PM
Declare global variable as a flag. In timer event handler, check the flag to decide run or stop.
mase
August 26th, 2008, 03:52 PM
Declare global variable as a flag. In timer event handler, check the flag to decide run or stop.
I think I tried that. I belive fileFound is a global variable, but thing is in this function
private void call_searchFile()
{
richTextBox1.AppendText("Calling to search_file()");
search_File();
if(fileFound== true)
richTextBox1.AppendText("\nFinish search file, do next task");
}
my second if-statement never gets executed because it doesn't wait for the search_File() to finish its task so it just come straight down to that if-statement while fileFound is false.
jasonli
August 26th, 2008, 04:47 PM
Your timer event handler is time_Tick, not call_searchFile.
mase
August 26th, 2008, 05:30 PM
Oh sorry I misread your respond. So you want me to check the flag inside the time_tick event hmmm, that should be work, but the problem is my project is controlled by the form interface so I'm not sure if I can put all of my other block of codes in there, because it doesn't build around Time event. Time event is only used when searching for file and this event will get called alot by other functions.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.