private static void keepTime()
{
System.Timers.Timer timer2 = new System.Timers.Timer();
timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Tick);
timer2.Start();
}
private static void timer2_Tick(object obj, ElapsedEventArgs e)
{
//elapse 4 sec
int intTime = 0;
while(intTime < 4)
{
intTime++;
}
if(intTime >= 4)
{
timer2.Stop();
}
}
this is pretty much the timer im using as of now...doesn't serve desired purpose so any sort of timer is fine...Using VS 2003
using timer to elapse time before method?
Started by gaylo565, May 01 2008 11:18 AM
9 replies to this topic
#1
Posted 01 May 2008 - 11:18 AM
I am currently working on a project with a timer in it. I am having trouble getting this part of my project to work. I need it to pause for a few seconds before it runs the next method. I can't just place the next method in the tick handler because it needs several variables to be passed to it. I could make the next method into its own class but I need it to modify lots of controls on the current form and this would be the long way around. Is there a way I can make a tick handler return a booleen so that once the time elapses I can run my next method? Any other simple solutions?
|
|
|
#2
Posted 01 May 2008 - 12:20 PM
The Timer does not work in this way. What you do is set the timer's Interval property to 4000 (specified in milliseconds):
Then, when the four seconds is up, the timer automatically calls the timer2_tick() event. Therefore, you don't need any of the code in that method - it calls it automatically after 4 seconds.
Xav
System.Timers.Timer timer2 = new System.Timers.Timer(); timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Tick); [B]timer2.Interval = 4000;[/B] timer2.Start();
Then, when the four seconds is up, the timer automatically calls the timer2_tick() event. Therefore, you don't need any of the code in that method - it calls it automatically after 4 seconds.
Xav
#3
Posted 01 May 2008 - 03:10 PM
this is a little better way to write it yes...but not really helping with my problem. I need to elapse time but carry the variable values over from the method which calls the timer to the code after the timer. I could just keep the same method going but I don't know how to make the timer pause the other method. The other option is to start another method in the tick handler with the next code to execute. I don't know how to pass the values into the tick handler to use as parameters for the next method call.
Edited by gaylo565, 01 May 2008 - 10:00 PM.
#4
Posted 02 May 2008 - 07:47 AM
I don't see any variables to carry over. What are they?
You can declare any variable as "private", as in:
You do not place this code within the method - instead, you put it just outside it, wherever you want, as long as it is in the same section as the methods, so you could put it just before the words "private static void keepTime()". To use it, just set the variable like a normal one - it can then be accessed and changed within any method.
Does this answer your problem?
You can declare any variable as "private", as in:
[B]private[/B] string myText = "This is a global, or module-level variable.";
You do not place this code within the method - instead, you put it just outside it, wherever you want, as long as it is in the same section as the methods, so you could put it just before the words "private static void keepTime()". To use it, just set the variable like a normal one - it can then be accessed and changed within any method.
Does this answer your problem?
#5
Posted 02 May 2008 - 09:02 AM
The problem is that i need to pass the values for the variables assigned by the previous method not just generic values.
#7
Posted 03 May 2008 - 08:48 AM
I got it working using a thread Sleep method. Thank you for your help xav. sometimes my posts don't make much sense i know, I musnt have read the last one before I posted.
#8
Posted 04 May 2008 - 06:30 AM
That's OK. I don't think the Thread.Sleep() method is the best choice, though - this method suspends the thread on which the program is run on. Therefore, during the 4 seconds the window completely freezes up, so the user cannot access it. A timer executes the time on a separate thread, so the user interface does not freeze up.
#9
Posted 04 May 2008 - 07:24 AM
It seems to work for the purpose...I could always start a new thread and do a sleep on the secondary thread as well. I was just having to many problems with the timer. I could get it to pause but not before it re-enabled my buttons. Im sure there is a way to make it work for my app but i'm tired of trying and need to get this done. Once again thanks for your help...im sure ill come up with another q for ya soon if not today.


Sign In
Create Account


Back to top









