Link to home
Start Free TrialLog in
Avatar of Manuel Lopez-Michelone
Manuel Lopez-MicheloneFlag for Mexico

asked on

Application percentage use

hi guys,

I want to write a program to find how many hours  I have an application active. The idea is to write an application who tracks which application is active all the time. For instance, suppose I am using delphi and ms messenger. My work is done in delphi, but probably I could spend most of the day chatting in ms messenger. If at the end of the day I can consult some sort of chart who tells me for how long I used all the applications I opened in my day session, I think this could be cool!

Is there a way to konw which application is active at any time?
regards
Manuel Lopez (lopem)
ASKER CERTIFIED SOLUTION
Avatar of ZhaawZ
ZhaawZ
Flag of Latvia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CodedK
:) Yes its a nice idea.
You could use a timer and get the list of the running applications.
Remove windows known system files from the list.
Remove duplicate entries from the list.
Timer will count the seconds the list has been unchanged...
BUT i dont have a clue how can you do that...

So i'm listening. :)
I think it would be much easier if you target 1 or 2 programs to monitor.
>> I think it would be much easier if you target 1 or 2 programs to monitor.
actually it does not matter ;) As I understood, he needs a way to monitor how much time a window / app is active (i.e., not running in background, but "has a focus") - in some cases it's easier to make it global, not only for few apps

Here's an example of using SetWindowsHookEx() to capture window-switching (which imho is better than timer):
https://www.experts-exchange.com/questions/21803642/Capture-application-switching.html
Avatar of Manuel Lopez-Michelone

ASKER

In http://www.swissdelphicenter.ch/torry/showcode.php?id=2143

I found this:  

function ActiveCaption: string;
var
  Handle: THandle;
  Len: LongInt;
  Title: string;
begin
  Result := '';
  Handle := GetForegroundWindow;
  if Handle <> 0 then
  begin
    Len := GetWindowTextLength(Handle) + 1;
    SetLength(Title, Len);
    GetWindowText(Handle, PChar(Title), Len);
    ActiveCaption := TrimRight(Title);
  end;
end;

{ - - - - - - - - - - - - - - - - - - - - -}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Label1.Caption := ActiveCaption;
end;

It seems pretty straightforward. I don't need to make a list of active processes. I just want to know which window is the active one... CodedK, this is the heart of the application I have in mind...

regards
Manuel Lopez (lopem)
This is simple and perfect :) but you need to track the time the appliaction stayed active !


In the timer you should add some count..

variable I : Integer;
Keep1, Keep2 : String;
Timer Interval 30 seconds...

On Timer :
---------------
Increase I.

Keep1 := ActiveCaption;
If Keep1<>Keep2 then
Begin
 Keep2:=Keep1;
 Add Keep2 to a List + Time (In seconds).
End;
-------------------------------------------------

but i'm not the author, lopem i'm just listening...
Correction :

On Timer :
---------------
Inc(I);
Keep1 := ActiveCaption;

If Keep1<>Keep2 then
Begin
  Keep2:=Keep1;
  Add Keep2 to a List + Time (In seconds).
End
else
Begin
  Remove current line from the list                   ----> (Example : Remove line    MSN 300 seconds)
  Add Keep1 to a List + Time (In seconds).        ----> (Write line : MSN 330 seconds)                
End;
---------