Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 1st, 2008, 2:15 PM   #1
ghg5
Newbie
 
Join Date: Apr 2008
Location: flagstaff az
Posts: 19
Rep Power: 0 ghg5 is on a distinguished road
getting timer to return booleen variable?

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
ghg5 is offline   Reply With Quote
Old May 1st, 2008, 3:17 PM   #2
Freaky Chris
Expert Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 771
Rep Power: 10 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: getting timer to return booleen variable?

Could you not use a global variable, and then in the on tick event set it equal to what every value suits you, then check to see if it is set, if so call you method or whatever it is you wish to do?

Chris
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote
Old May 1st, 2008, 3:32 PM   #3
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 335
Rep Power: 12 kruptof is on a distinguished road
Re: getting timer to return booleen variable?

Try using the interval property of the Timer class.
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old May 2nd, 2008, 12:01 AM   #4
ghg5
Newbie
 
Join Date: Apr 2008
Location: flagstaff az
Posts: 19
Rep Power: 0 ghg5 is on a distinguished road
Re: getting timer to return booleen variable?

the problem I am having is that i need to pass the values for variables form the method that calls the timer into the method I want to run when the time is elapsed. The tick handler wont take any arguments unless I change the arguments in the method handler but then I get an error because it doesn't match the args in the event handler bind. I am not a great programmer and maybe im just missing something super obvious, but I have tried all sorts of things. I cant get the timer to make the other method pause, and I cant pass the variables through if I start a new method.
ghg5 is offline   Reply With Quote
Old May 2nd, 2008, 2:48 AM   #5
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 335
Rep Power: 12 kruptof is on a distinguished road
Re: getting timer to return booleen variable?

If you are using a timer just for the sake of waiting then just put the thread to sleep for 4 seconds. IF you require some activity on the main thread whilst waiting then just create a new thread and then sleep that for 4 seconds. IF you require sleeping more than once then have a look at locking and waiting on an object in a loop. Have a look at this book.
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old May 2nd, 2008, 8:51 AM   #6
Alias
Hobbyist Programmer
 
Join Date: Oct 2007
Location: Newcastle upon Tyne, UK
Posts: 113
Rep Power: 10 Alias is on a distinguished road
Send a message via MSN to Alias Send a message via Skype™ to Alias
Re: getting timer to return booleen variable?

Can you post more code? Im thinking your own event/delegate would help here, however I don't have enough of your code to find out for sure.
Alias is offline   Reply With Quote
Old May 2nd, 2008, 11:25 AM   #7
ghg5
Newbie
 
Join Date: Apr 2008
Location: flagstaff az
Posts: 19
Rep Power: 0 ghg5 is on a distinguished road
Re: getting timer to return booleen variable?

here is the code...its for a memory game and i need the button to stay disabled and non visible for a few seconds so that the user can see the second pic even if they guessed wrong. I was hoping to figure it out on my own with a bit of help but i am frustrated now and am just posting what i have...I got rid of my timer for now and am messing around with the threading idea. Its kind of new to me though and I am getting all sorts of error messages in the code Ive written...takes me a bit to debug.
		public void checkMatch(int intCount3, int intCount2, int intCount4, int ingCount5)
		{
			blnMatch = false; 
			if(intCount2 == 1 || intCount2 == 2 || intCount2 == 3 || intCount2 == 4 || intCount2 == 5 || intCount2 == 6)
			{
				
				if(intCount3 == 1 || intCount3 == 2 || intCount3 == 3 || intCount3 == 4 || intCount3 == 5 || intCount3 == 6)
				{
					blnMatch = true;
				}
				
				else
				{
					intGuess ++;
					blnMatch = false;
				}
			}
			
			else if(intCount2 == 7 || intCount2 == 8 || intCount2 == 9 || intCount2 == 10 || intCount2 == 11 || intCount2 == 12)
			{
				if(intCount3 == 7 || intCount3 == 8 || intCount3 == 9 || intCount3 == 10 || intCount3 == 11 || intCount3 == 12)
				{
					blnMatch = true;
				}
				else
				{
					intGuess++;
					blnMatch = false;
				}
			}
			
			else if(intCount2 == 13 || intCount2 == 14 || intCount2 == 15 || intCount2 == 16 || intCount2 == 17 || intCount2 == 18)
			{
				if(intCount3 == 13 || intCount3 == 14 || intCount3 == 15 || intCount3 == 16 || intCount3 == 17 || intCount3 == 18)
				{
					blnMatch = true;
				}
				else
				{
					intGuess++;
					blnMatch = false;
				}
			}
			
			else if(intCount2 == 19 || intCount2 == 20 || intCount2 == 21 || intCount2 == 22 || intCount2 == 23 || intCount2 == 24)
			{
				if(intCount3 == 19 || intCount3 == 20 || intCount3 == 21 || intCount3 == 22 || intCount3 == 23 || intCount3 == 24)
				{
					blnMatch = true;
				}
				else
				{
					intGuess++;
					blnMatch = false;
				}
			}
			
			else if(intCount2 == 25 || intCount2 == 26 || intCount2 == 27 || intCount2 == 28 || intCount2 == 29 || intCount2 == 30)
			{
				if(intCount3 == 25 || intCount3 == 26 || intCount3 == 27 || intCount3 == 28 || intCount3 == 29 || intCount3 == 30)
				{
					blnMatch = true;
				}
				else
				{
					intGuess++;
					blnMatch = false;
				}
			}
			
			else if(intCount2 == 31 || intCount2 == 32 || intCount2 == 33 || intCount2 == 34 || intCount2 == 35 || intCount2 == 36)
			{
				if(intCount3 == 31 || intCount3 == 32 || intCount3 == 33 || intCount3 == 34 || intCount3 == 35 || intCount3 == 36)
				{
					blnMatch = true;
				}
				else
				{
					intGuess++;
					blnMatch = false;
				}
			}
			
			else if(intCount2 == 37 || intCount2 == 38 || intCount2 == 39 || intCount2 == 40 || intCount2 == 41 || intCount2 == 42)
			{
				if(intCount3 == 37 || intCount3 == 38 || intCount3 == 39 || intCount3 == 40 || intCount3 == 41 || intCount3 == 42)
				{
					blnMatch = true;
				}
				else
				{
					intGuess++;
					blnMatch = false;
				}
			}
			if(blnMatch == false)
				//This is where i need to elapse time
				{
				if(intCount4 == 0)
				{
					btn0.Visible = true;
					btn0.Enabled = true;
				}
				if(intCount5 == 0)
				{
					btn0.Visible = true;
					btn0.Enabled = true;
				}
				if(intCount4 == 1)
				{
					btn1.Visible = true;
					btn1.Enabled = true;
				}
				if(intCount5 == 1)
				{
					btn1.Visible = true;
					btn1.Enabled = true;
				}
				if(intCount4 == 2)
				{
					btn2.Visible = true;
					btn2.Enabled = true;
				}
				if(intCount5 == 2)
				{
					btn2.Visible = true;
					btn2.Enabled = true;
				}
...there are a whole bunch more if and else if statements after this which need the variables as well.
ghg5 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning a value from a variable to the main function colt C 4 Jun 20th, 2009 3:26 PM
FTP and return code fetching Serinth C 2 May 28th, 2006 11:05 PM
using `char' return type in functions atreyu C 6 Jul 22nd, 2005 12:13 PM
How to program a timer? linuxpimp20 Other Programming Languages 9 Jul 5th, 2005 4:32 AM
variable problem robert_sun C 1 Apr 12th, 2005 2:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:41 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC