Your question is not clear but I believe what you want to use is a
Sleep
statement in your
for
loop. That would look something like this :
for( int n = 0; n < loopCount; ++n )
{
Sleep( delayInMilliseconds );
}
That kind of thing is not recommended but it can work.
A better option is to use a timer and in your OnTimer handle iterations of the loop. In effect, this is unrolling the loop. That can look like this :
void MyClass:OnTimer( UINT_PTR timerId )
{
if( timerId == m_MyTimerId )
{
if( m_Looping )
{
DoLoopIteration( m_LoopIndex );
++m_LoopIndex;
if( m_LoopIndex >= m_LoopCounts )
{
m_Looping = false; }
}
}
__super::OnTimer( timerId );
}
In this code,
OnTimer
is a method of your window or dialog class and the variables
m_MyTimerId
,
m_Looping
,
m_LoopIndex
, and
m_LoopCounts
are all members of that class and are used to hold the loop's parameters.
You could make a separate loop structure to contain them if you prefer that organization or if you had multiple loops to process. That could look like this :
structure LoopData
{
UINT_PTR m_TimerId;
bool m_Looping;
int m_LoopIndex;
int m_LoopCounts;
void Start( UINT_PTR timerId, int count )
{
m_TimerId = timerId;
m_LoopCounts = count;
m_LoopIndex = 0;
m_Looping = true;
}
bool Update( UINT_PTR timerId )
{
if( timerId != m_TimerId )
return false; if( ! m_Looping )
return false;
++m_LoopIndex;
if( m_LoopIndex >= m_LoopCounts )
{
m_Looping = false;
return false; }
return true; }
};
I added a few methods to the structure to handle maintenance of the loop. With a structure like this, the
OnTimer
handler would look like this if it was handling multiple timers :
void MyClass:OnTimer( UINT_PTR timerId )
{
if( m_Timer1.Update( timerId ) )
DoLoopOneIteration( m_Timer1.m_LoopIndex );
else if( m_timer2.Update( timerId )
DoLoopTwoIteration( m_Timer2.m_LoopIndex );
__super::OnTimer( timerId ); }
You would want to use the timer method if this code is repeatedly executed. If it is a one-time thing in initialization then using using
Sleep
in the
for
loop is OK.