![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2008
Location: flagstaff az
Posts: 18
Rep Power: 0
![]() |
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();
}
} |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
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
__________________
Who said i couldn't program sarcasm = raw_input('Type in a sarcastic remark: ')
|
|
|
|
|
|
#3 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3
![]() |
Re: getting timer to return booleen variable?
Try using the interval property of the Timer class.
__________________
Quote:
|
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2008
Location: flagstaff az
Posts: 18
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#5 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3
![]() |
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:
|
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Oct 2007
Posts: 29
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Apr 2008
Location: flagstaff az
Posts: 18
Rep Power: 0
![]() |
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;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 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 |
| Returning a value from a variable to the main function | colt | C | 3 | Apr 28th, 2005 7:56 AM |
| variable problem | robert_sun | C | 1 | Apr 12th, 2005 2:10 PM |