If you are reading this text then most probably you have your computer turned on. But for how long? You can get the time for how long the computer is running for.
The operating system, such as Windows, Ubuntu, Mac OS, keeps a record of how long the computer is running for. We can simply get the running time and use it in our program.
Each Operating System (OS) has different ways of keeping this record and a different way of retrieving it. For example, in Windows it can be retrieved with the help of Windows API. Fortunately, in Lazarus, we don’t have to get into these mumbo jumbo for each platform. We can use windows.GetTickCount
for windows or LclIntf.GetTickCount
for a cross platform way. (We would have to add windows
or LclIntf
in the uses clause as well.) We will see the both ways. But for both of them, the syntax is the same:
1 | function GetTickCount: DWord; |
The GetTickCount()
procedure returns the running time in milliseconds. 1 Seconds = 1000 Milliseconds. So we can divide the returning value by 1000
to get the running time in Seconds. DWord
is not new for API based functions and procedures. There is nothing to worry about. We would use Variant
type to store the returing value of DWord
. We could then use it as normal float
value.
The Windows way (Not recommended)
We can use the GetTickCount()
from the Windows
unit to get running time in Windows. It utilizes Windows API so it will only work in Windows and not other platforms, such as Linux and Mac. It is not recommended to use it for this reason. But it is interesting to know this stuff anyway!
Create a new Application Project (Project -> New Project -> Application -> OK).
Switch to code view (F12) and add windows
to the uses
section:
1 | uses |
Double click the form and enter:
1 | procedure TForm1.FormCreate(Sender: TObject); |
We have used TickCount Div 1000
to get the time in seconds. We have used Div
to get an integer
value.
Run the project with F9 (or Run -> Run).
The seconds would obviously differ from this screenshot because the running time will not be the same with your computer. But if you see a number it is working.
The Cross-platform way (Recommended)
This method is more accepted because it works in all platforms including Windows.
Create a new Application Project (Project -> New Project -> Application -> OK). Now switch to code view and add the LclIntf
to the uses
clause:
1 | uses |
Now double click the form and enter:
1 | procedure TForm1.FormCreate(Sender: TObject); |
Now Run the project. The result would be similar to the above.
Demo Project
The above is for just testing. It won’t sell, right? We have to make something better in order to satisfy our users, because they will be hungry for more.
We would create a simple program to show the running time in hours:minutes:seconds which would update every second to show the actual running time every second.
Create a new Application project (Project -> New Project -> Application -> OK).
Drop a TLabel on your form. Make its font large enough (set Font->Size property to something like 18
). Set its Name
as lblTime
. You can change its Caption
to 0:00:00
. Now place a TTimer (from System tab) in your form. By default it should be enabled and its interval should be set to 1000
milliseconds (which equals to 1 second). Double click Timer1
and enter:
1 | procedure TForm1.Timer1Timer(Sender: TObject); |
Explanation:
1 | hrs := (mySeconds Div 60) Div 60; |
First we calculate the hours. We know, Hour = (Seconds / 60) / 60
1 | mySeconds := mySeconds - (hrs * 60 * 60); |
After calculating the hours, we eliminate the calculated hours from the seconds value. Now we have to calculate minutes and seconds from this remaining time.
1 | min := mySeconds Div 60; |
Now we calculate minutes. We know, Minutes = Seconds / 60.
1 | mySeconds := mySeconds - (min \* 60); |
After calculating minutes we subtract the calculated minutes from MySeconds
. Now we are left with seconds. So…
1 | sec := mySeconds; |
Now, decorate your form with other explanatory labels. Resize the form to appropriate size. To disable the maximize button set the Form’s BorderIcons -> biMaximize to False
. To make it un-resizable set its BorderStyle
property to bsSingle
.
Now Run the project (F9 or Run -> Run).
You can take further challenges with this code. It is great for learning new stuff and making your learning permanent. You can save the user’s daily computer usage in a text file or database (maybe with Sqlite). You can also create a statistics form and show for how much time the user keeps the computer running in a week, month or even a year. This could be an interesting app. Also if you could keep it synchronized with multiple computers (for example, across desktops and laptops and even multiple platforms) then it would be a delicacy!
Downloads
You can download the source code for the tutorial project and executable/EXE files from the links below: