Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C# Programming

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-01-2008, 02:18 PM
gaylo565's Avatar   
gaylo565 gaylo565 is offline
Learning Programmer
 
Join Date: May 2007
Location: flagstaff, az
Posts: 65
Last Blog:
String Manipulation wi...
Rep Power: 6
gaylo565 will become famous soon enoughgaylo565 will become famous soon enough
Default using timer to elapse time before method?

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?
Code:
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-01-2008, 03:20 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: using timer to elapse time before method?

The Timer does not work in this way. What you do is set the timer's Interval property to 4000 (specified in milliseconds):

Code:
System.Timers.Timer timer2 = new System.Timers.Timer();
			timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Tick);
timer2.Interval = 4000;
			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
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-01-2008, 06:10 PM
gaylo565's Avatar   
gaylo565 gaylo565 is offline
Learning Programmer
 
Join Date: May 2007
Location: flagstaff, az
Posts: 65
Last Blog:
String Manipulation wi...
Rep Power: 6
gaylo565 will become famous soon enoughgaylo565 will become famous soon enough
Default Re: using timer to elapse time before method?

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.

Last edited by gaylo565; 05-02-2008 at 01:00 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-02-2008, 10:47 AM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: using timer to elapse time before method?

I don't see any variables to carry over. What are they?
You can declare any variable as "private", as in:

Code:
private 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?
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-02-2008, 12:02 PM
gaylo565's Avatar   
gaylo565 gaylo565 is offline
Learning Programmer
 
Join Date: May 2007
Location: flagstaff, az
Posts: 65
Last Blog:
String Manipulation wi...
Rep Power: 6
gaylo565 will become famous soon enoughgaylo565 will become famous soon enough
Default Re: using timer to elapse time before method?

The problem is that i need to pass the values for the variables assigned by the previous method not just generic values.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-02-2008, 12:11 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: using timer to elapse time before method?

I don't follow. Why do you need the variables, and not the values?
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-03-2008, 11:48 AM
gaylo565's Avatar   
gaylo565 gaylo565 is offline
Learning Programmer
 
Join Date: May 2007
Location: flagstaff, az
Posts: 65
Last Blog:
String Manipulation wi...
Rep Power: 6
gaylo565 will become famous soon enoughgaylo565 will become famous soon enough
Default Re: using timer to elapse time before method?

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-04-2008, 09:30 AM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: using timer to elapse time before method?

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.
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-04-2008, 10:24 AM
gaylo565's Avatar   
gaylo565 gaylo565 is offline
Learning Programmer
 
Join Date: May 2007
Location: flagstaff, az
Posts: 65
Last Blog:
String Manipulation wi...
Rep Power: 6
gaylo565 will become famous soon enoughgaylo565 will become famous soon enough
Default Re: using timer to elapse time before method?

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-04-2008, 10:40 AM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: using timer to elapse time before method?

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!
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Shortcut List - Saves Time 2stamlers The Lounge 6 04-10-2008 06:58 AM
A simple timer travy92 VB Tutorials 1 02-18-2008 09:08 AM
Minimum time to make live a Website in Internet eric Search Engine Optimization 3 11-25-2007 01:09 PM
Minimum time to make live a Website in Internet Jaser Search Engine Optimization 2 11-08-2007 11:44 PM
Odd Timestamp dirkfirst C# Programming 10 12-08-2006 04:40 PM


All times are GMT -5. The time now is 03:20 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads