Jump to content

using timer to elapse time before method?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
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?

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

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
The Timer does not work in this way. What you do is set the timer's Interval property to 4000 (specified in milliseconds):

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
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
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
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I don't see any variables to carry over. What are they?
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?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
The problem is that i need to pass the values for the variables assigned by the previous method not just generic values.

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I don't follow. Why do you need the variables, and not the values?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
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
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
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.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
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.

#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
You're welcome - when you want to learn how to use timers properly, you can always do it in your own time. Good luck with your program!
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums