How to display time elapsed continuously while a lengthy task is running?

On Linux I run a C++ program that invokes 3 tasks one by one like this:

int main()
{
task1();
task2();
task3();
return 0;
}

Each task may take about 5 minutes to complete. When a task is running, Log messages will be displayed on screen one by one.

What I'm looking for is a way to continuously display the time elapsed (in seconds) at the bottom of the screen (use one line at the bottom) when a task is running.

I would appreciate it if some one could help. A sample code would be very helpful.

Thanks!
Last edited on
If each task is a loop, you could have something like
1
2
if (!(i%1000))
    std::cout <<clock()<<std::endl;
http://cplusplus.com/reference/clibrary/ctime/clock/


i guess u know how to do it with ascending numbers :P...
Last edited on
You can do it the way helios proposed if you are willing to mix the task's code with user interface code. Personally, I don't like that. But, as all in life, comes with a cost: Multithreading.

I would spawn a worker thread to carry out the tasks, and then the main thread would become a loop that checks whether the worker thread continues to work. If it is working still, then I would update the on-screen elapsed time. The loop would continue until the worker thread finishes.

I cannot help further because I don't know the differences between Linux and Windows, and I'm a Windows-only guy. In Windows I would use the CreateThread() function, and then I would wait on the thread handle with WaitForSingleObject() with a timeout of say, 500 milliseconds, so that every 500 milliseconds I get to refresh the screen.
Read the book:"MUD Game Programming(2004)" and modified its accompanying source code under "Libraries\BasicLib\BasicLibTime(.h and .cpp)", which should be portable on both Linux and Windows. Multithreading would do both tasks you want.
Thanks for the suggestions. Two follow-up questions:

Q1:
I agree I need to use another thread. What I'm not sure is how to re-use the line at the bottom of screen to update the elapsed time while take1() keeps popping up log messages to screen.

It doesn't look good if the application displays a new lines for each loop for show the elapsed time (like http://cplusplus.com/reference/clibrary/ctime/clock/ does).

Q2:
Google search doesn't give me any links where I can read "MUD Game Programming(2004)" or its source code. I only found web sites that try to sell this book. Is the actual e-book available for us to read somewhere?

Many thanks!

Q1: For how to do it on Windows, read the Book "Programming Windows (5Edit) by Chrles Petzold" ->Chapter 20: Multitasking and Multithreading + its source code. I am sure you can figure out how to do it on Linux depending how you want to display the time stamp on the screen once you got the source code of the MUD book.

Q2: Yes, you must buy or borrow the book. There is no eBook available. PM me for its src code.
Last edited on
are you doing this console- or window-style?...

console:

get the cursor info for the console and set the actual cursor position back to the position where ur time has to be displayed...

SetConsoleCursorPosition(stdoutputhandle,coordinates);...
On Windows console (command window) (Code 1):
 
http://www.cplusplus.com/forum/beginner/4234/ 


Good news: on Windows console I have successfully displayed the time stamp at a location and update it (clearing it, then displaying it at the same location) every 5 seconds using (Code 2) boost/thread/xtime.hpp.

I am sure that using (Code 3) boost/thread/thread.hpp, which supports multithreading, what you want to do can be done on Windows console. (Multithreading: using one worker thread doing task1, 2, 3 sequentially and use main thread updating the time stamp on the console at predefined time interval.)

Code 1 + Code 2 + Code 3 + Code 4(MUD library) is the solution on Windows.

BTW: Boost C++ is supported on Linux / Unix. So you only need to figure out Code 1 for Linux yourself.
Last edited on
Thanks for your help, all of you!

Robertlzw:

It seems your account doesn't accept Personal Message. Is it possible to post or email me the src code you created for displaying time stamp at a location and update it after 5 sec? I'm a newbie in C++ so a sample code would be very helpful.

Many thanks.
The MUD libraries in the book include many additional features that we do not need for solving the question of this thread. It is an overkill and would be difficult to understand without the book. So I would rather not post my code here.
Last edited on
The source code for the book:"MUD Game Programming(2004)" can be downloaded at:
 
http://www.2shared.com/file/4874891/a6600879/MUD_Game_Programming.html 
Last edited on
Thanks. I'll download the code and try to figure it out.
Topic archived. No new replies allowed.