Example #1
0
//============================================================================
//		NThreadUtilities::DelayFunctor : Delay a functor.
//----------------------------------------------------------------------------
void NThreadUtilities::DelayFunctor(const NFunctor &theFunctor, NTime theDelay, bool onMainThread)
{	NTimer		*theTimer;



	// Invoke immediately
	//
	// If we're to invoke on a new thread, or are already on the main
	// thread, we can invoke the functor directly without any delay.
	//
	// If we can't (we have a delay or we're not the main thread but
	// the functor must execute on the main thread), we fall through
	// to the timer case.
	if (NMathUtilities::IsZero(theDelay))
		{
		if (!onMainThread)
			{
			DetachFunctor(theFunctor);
			return;
			}
		
		else if (NThread::IsMain())
			{
			theFunctor();
			return;
			}
		}



	// Invoke with a delay
	theTimer = new NTimer;
	if (theTimer != NULL)
		theTimer->AddTimer(BindFunction(NThreadUtilities::DelayedFunctor, theTimer, theFunctor, onMainThread), theDelay);
}