Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Timer control

Tags:

c++

I want to create a timer so that after completing the time(suppose 10 sec) the control should come out of the function..Please note that am starting the timer inside the function.Code is given below..I want to give certain time limit to that function so that after completing the time the control should come out of the function..I don't want to calculate the time..I want to give my own time so that the function should complete its execution within that time period..suppose if function is waiting for an input then also after completing time limit the control should come out indicating that "time has expired"..once it comes out of the function then it should continue with the next function execution...Is this possible in c++...

Begin();

// here I would like to add timer.

v_CallId = v_CallId1;
call_setup_ind();
call_alert_ind();
dir_read_search_cnf();
dir_save_cnf();

END();
like image 747
kumar Avatar asked Feb 24 '26 12:02

kumar


1 Answers

If the code is linear and the functions called cannot be chopped into smaller pieces, your stuck to letting an external process/thread do the timing and abort the worker thread when the timeout is exceeded.

When you can chop the worker into smaller pieces you could do something like this

Timeout.Start(5000);
while ((TimeOut.TimeOut() == false) && (completed == false))
{
   completed = WorkToDo()
}

This is a pattern we frequently use in our embbeded application. The timeout class was in house develop. It just reads the tick counter and looks if the time has passed. An framework like QT or MFC should have such a class itself.

like image 142
refro Avatar answered Feb 27 '26 01:02

refro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!