[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Application percentage use

Posted on 2006-04-05
7
Medium Priority
?
139 Views
Last Modified: 2012-05-05
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)
0
Comment
Question by:lopem
  • 4
  • 2
7 Comments
 
LVL 11

Accepted Solution

by:
ZhaawZ earned 375 total points
ID: 16389186
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
 
LVL 16

Expert Comment

by:CodedK
ID: 16419158
:) 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
 
LVL 16

Expert Comment

by:CodedK
ID: 16419164
I think it would be much easier if you target 1 or 2 programs to monitor.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:ZhaawZ
ID: 16420014
>> 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
 
LVL 3

Author Comment

by:lopem
ID: 16423145
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
 
LVL 16

Expert Comment

by:CodedK
ID: 16424361
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
 
LVL 16

Expert Comment

by:CodedK
ID: 16424400
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question