Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by David_O ( 6 years ago )
/* 
-----------------------------------------------------------------------------
	Method description: This method tries to execute a pre-defined function 
	several times until it succeeds or reaches maxInterval time  
----------------------------------------------------------------------------

	Comments: #Consider adding sanity check for Uint overflow
			  #Consider adding executeSomeMagic as input parameter 
			   instead of hard-coding the function
 */

void tryExecute(unsigned int intervalStep, unsigned int maxInterval)
{
	
	bool executeSuccess = 0;

	//accept only positive integers
	if(intervalStep > 0 && maxInterval > 0)
	{
		const unsigned int maxIntervalWatch = maxInterval;
		
		//step sanity check
		if(intervalStep <= maxInterval)
		{
			//try to execute executeSomeMagic() until 
			//it runs out of set time or is successfully executed
			while(maxInterval - intervalStep >= 0 || !executeSuccess)
				{
					try
					{
						//to ensure loop ends
						maxInterval = maxInterval - intervalStep;
						
						// Bad name for function - what does it do?
						// Consider refactoring
						executeSomeMagic();
						
						// exception was not thrown, the function was executed
						executeSuccess=1;
				
					}
					// exception was caught, so just wait for another interval
					catch
					{
					Thread.Sleep(intervalStep);
					}
				}
				if (executeSuccess)
				{
					printf("Execute finished after %u milliseconds", intervalStep);	
				}
				else 
				{
					printf("Execute failed after %u milliseconds", maxIntervalWatch);	
				}
		}
		else
		{
			printf("intervalStep must be a lower integer number than maxInterval");
		}	
			
	}
	else
	{
		printf("input must be a positive integer higher then zero");
	}
}

 

Revise this Paste

Children: 105287
Your Name: Code Language: