Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I must use a cycle for and inside I must insert a timer, but I saw that cycle doesn't block with timer but it continue..how must I do to block for cycle?

What I have tried:

I tried what I wrote above..but I didn't solve
Posted
Updated 26-Jul-23 7:48am
Comments
Richard MacCutchan 26-Jul-23 13:58pm    
You have posted enough questions here to know that we cannot guess what your code is doing. Please edit your question and add proper details.

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 :
C++
for( int n = 0; n < loopCount; ++n )
{
    // do something here

    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 :
C++
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;             // terminate the loop
            }
        }
    }
    __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 :
C++
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;    // not our timer
         if( ! m_Looping )
             return false;    // we are not looping

         ++m_LoopIndex;
         if( m_LoopIndex >= m_LoopCounts )
         {
             m_Looping = false;
             return false;    // loop has ended
         }
         return true;         // loop is still active
    }
};
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 :
C++
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 );    // let the base class have its turn
}
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.
 
Share this answer
 
v3
If I understood the question correctly, should a loop be blocked until a timer allows it to continue running? Blocking an MFC program with Sleep() is usually not a useful option.

Unfortunately we don't acquire the purpose for which this behavior is needed. If something should happen on each loop pass, it might make more sense to initialize a count variable first, then start a system timer. The timer could call a function that takes care of the change and then increments the counter until a final value is reached. Finally remove the system timer again. This is similar to Rick's suggestion.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900