Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys,
i have a problem please, i want to run a timer with 20 milliseconds interval but a method (methodA) calls it and start it. it is needed that timer should complete its task after 10 execution ( means that the total time will be 200 millisecond for a task).

i want that methodA should wait for the 10 execution of the timer please.

any idea please, or what could be the best proper solution.

thanks
Posted
Comments
CHill60 4-Jul-14 11:34am    
Why not set the timer for 200ms??
Muhammad Tufail 1979 4-Jul-14 11:42am    
cause 200ms are for the whole task. the task is to move an object (i.e picture) from one place to another place in the panel within 10 interval and thus the time will be for each move should be 20ms.
Rob Philpott 4-Jul-14 11:37am    
I hope you don't want an accurate 20ms, because the timer is unlikely to give you that.
Muhammad Tufail 1979 4-Jul-14 11:44am    
could you please explain me a bit more. Actually i want to keep the time constant (i.e 20ms) for each iteration, so thus the moving of picture will be smooth.
[no name] 4-Jul-14 11:39am    
Okay so what is the specific question here? Can you not implement a timer? Set a timer interval? Count to 10? Implement an autoresetevent?

1 solution

Easy! Create a class level counter, set it to the number of iterations, and count it down in the Tick event:
C#
    Timer myTimer = new Timer();
    myTimer.Interval = 20;
    myTimer.Tick += new EventHandler(myTimer_Tick);
    intervalCounter = 10;
    myTimer.Start();
    }
private int intervalCounter;

void myTimer_Tick(object sender, EventArgs e)
    {
    if (intervalCounter > 0)
        {
        intervalCounter--;
        if (intervalCounter == 0)
            {
            // Do what you want
            intervalCounter = 10;
            }
        }
    }
 
Share this answer
 
Comments
Muhammad Tufail 1979 4-Jul-14 11:58am    
it mean i don't need MethodA. i can do the functionality of methodA , when intervalCounter==0.
in the area of //Do what you want...
am i right please.
and it will make my program responsible if i have a button and to click on that to stop or start or pause this counter please.

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