Fog Creek Software
Discussion Board

VB.Net Code Help in logging user activity.

Hi All,

I am trying to make application similar to Worktime.

[No no, I am not trying to create a competing product but I want to create a simple application for my personal use + learn more of .NET]

Now, requirement is that this tool should capture the time user has spent on particular application. I looked in to Process class under System.Diagnostics namespace but it can tell me which processes are running, does it have window title etc. But I coudln't find property which indicates that this process is currently receiving user input/focuc.

Is there any object which gives me details about the application which user is interacting with? Also get event indicating that user has started using another application??

Any links/resources/sample code will be really helpful.


Saturday, November 22, 2003

Only one window at a time can receive focus. So check which application is active, as that is the window receiving focus.

Then time it.

Saturday, November 22, 2003

And that is what I want to know. How do I know which application is 'active'?

Let me relook at process class properties/methods.


Saturday, November 22, 2003

It's hard for me to believe that no one here as any pointers/suggestions to offer??

Guys, please help me!!!!!!


Sunday, November 23, 2003

How about the Windows API function called "GetForegroundWindow".

Christopher Wells
Sunday, November 23, 2003


I just found it before I could check the this forum again!

I will re-state that my aim is to find how much time user has spent on each of the window he has opened.

I am using timer control with interval of 1 second.
I am capturing foreground window and checking if I already have that 'Window Title' in my list. If yes, then I add one second to the time spent for that particular window.

Here's how I have done as of now.

    Declare Function GetActiveWindow Lib "user32.dll" () As Integer
    Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
    Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Integer) As Integer

    Declare Function GetForegroundWindow Lib "user32" () As Integer

    Private Sub tmrPoll_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrPoll.Tick
        Dim hwnd As Integer = GetForegroundWindow
        Dim sBuild As New System.Text.StringBuilder(GetWindowTextLength(hwnd) + 1)
        GetWindowText(hwnd, sBuild, sBuild.Capacity)

        Dim iSeconds As Integer

        If htApps.ContainsKey(sBuild.ToString()) Then
            htApps.Item(sBuild.ToString) = htApps.Item(sBuild.ToString) + 1
            htApps.Add(sBuild.ToString(), 0)
        End If

    End Sub

Have I picked up the correct way OR you think that same thing can be achieved in much better way?

Kindly share your views,

Sunday, November 23, 2003

What if the user goes for lunch or even leaves the computer on overnight - do you keep logging the window that they left open??

Monday, November 24, 2003

Just an idea. I guess you can also look at the mouse movement. Something like -- if no mouse movement for xx minutes that stop the clock...

Friday, November 28, 2003

*  Recent Topics

*  Fog Creek Home