Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Urgent! Pausing a function using a timer

Status
Not open for further replies.

chowki

Technical User
Joined
Nov 25, 2004
Messages
6
Location
GB
I have a function that executes on every second.
I wish to add an if statment that if it is true then it will do nothing for the next 5 seconds.

How can I do this?

public void MyFunction()

if (statement is true)
{
do not run through function for 5 seconds
}
 
This might help.
Code:
if (TimeDelay(5000)) { //5 seconds 
        //5 seconds is up	
}


public static bool TimeDelay(int intDelayInMilliSeconds)
{
	Thread.Sleep(intDelayInMilliSeconds);
	return true;
}
Marty
 
Could you explain this a bit please.

If I have:

public void MyFunction() //executes every second
if (condition1)
{//do this}
if (condition2)
{//do this}

if (a different statement is true)
{
do not run through function for 5 seconds
}

public static bool TimeDelay(int intDelayInMilliSeconds)
{
Thread.Sleep(intDelayInMilliSeconds);
return true;
}

Whereabouts does
if (TimeDelay(5000)) { //5 seconds
//5 seconds is up
}
go?
And is this bit of code saying do what is in it once the 5 seconds is up?
 
you will have to use System.Threading
using System.Threading;

if (a different statement is true)
{
//do not run through function for 5 seconds
if (TimeDelay(5000)) { //5 seconds
/*5 seconds is up and we are out of the if statement nothing has been done, all that happened is it stayed in TimeDelay for 5 seconds
*/
}
}

hth,
Marty
 
Great, thank you so much Marty.

I hadn't put the System.Threading line in.

Thank you again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top