|
-
August 25th, 2008, 01:07 PM
#1
Timer behavoir?
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.
Code:
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);
}
}
-
August 25th, 2008, 01:31 PM
#2
Re: Timer behavoir?
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.
Last edited by darwen; August 25th, 2008 at 01:33 PM.
-
August 25th, 2008, 01:47 PM
#3
Re: Timer behavoir?
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.
-
August 25th, 2008, 02:52 PM
#4
Re: Timer behavoir?
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.
-
August 25th, 2008, 03:02 PM
#5
Re: Timer behavoir?
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.
-
August 25th, 2008, 03:06 PM
#6
Re: Timer behavoir?
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.
-
August 25th, 2008, 03:29 PM
#7
Re: Timer behavoir?
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.
Last edited by mase; August 25th, 2008 at 03:32 PM.
-
August 25th, 2008, 03:45 PM
#8
Re: Timer behavoir?
Wait you said there is no such System.Timers? but I have been using it unless they are the same to windows.Forms
Code:
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
Last edited by mase; August 25th, 2008 at 04:05 PM.
-
August 26th, 2008, 08:39 AM
#9
Re: Timer behavoir?
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.
The difficulty is that you have no idea how difficult it is.
.Net 3.5/VS 2008
-
August 26th, 2008, 03:33 PM
#10
Re: Timer behavoir?
 Originally Posted by jasonli
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.
-
August 26th, 2008, 03:39 PM
#11
Re: Timer behavoir?
Is it ok to instantiate System.Threading.Timer in the constructor?
thanks.
Last edited by mase; August 26th, 2008 at 03:47 PM.
-
August 26th, 2008, 03:45 PM
#12
Re: Timer behavoir?
Declare global variable as a flag. In timer event handler, check the flag to decide run or stop.
The difficulty is that you have no idea how difficult it is.
.Net 3.5/VS 2008
-
August 26th, 2008, 03:52 PM
#13
Re: Timer behavoir?
 Originally Posted by jasonli
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
Code:
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.
-
August 26th, 2008, 04:47 PM
#14
Re: Timer behavoir?
Your timer event handler is time_Tick, not call_searchFile.
The difficulty is that you have no idea how difficult it is.
.Net 3.5/VS 2008
-
August 26th, 2008, 05:30 PM
#15
Re: Timer behavoir?
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|