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)
LVL 3
lopemAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZhaawZSoftware DeveloperCommented:
1) you may use GetForegroundWindw() each time you want to know which window is active, but this must be done many times (i.e., each time you want to get info)
2) you may use SetWindowsHookEx() function to set CBT hook; this will call your function each time when some CBT action occurs, for example, when any window is about to be activated (you'll get also a handle of window which is being activated), when a window is about to be created, moved etc; I think this is better sollution for your needs.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodedKCommented:
:) 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. :)
0
CodedKCommented:
I think it would be much easier if you target 1 or 2 programs to monitor.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ZhaawZSoftware DeveloperCommented:
>> 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):
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21803642.html
0
lopemAuthor Commented:
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)
0
CodedKCommented:
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...
0
CodedKCommented:
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;
---------
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.