APM (Action per minute) application vb.Net

Posted on 2012-09-18
Last Modified: 2013-04-30
Hello EE,

Like in Starcraft 2, in which there is a monitor for APM (Action per minute), Can you help create one in Vb.NET ?

In other words... its two little box where the right one is a number that indicates how many action you perform with keyboard / mouse in a minute... its like an average it can reach 300 if you are pretty fast (it can reach 300 in 3-4 secs because lets say you press the letters A and C repeatly fast, its does an average that it will reach 300 in a minute)

and the left box is the average of the Right box...

Can you help do that ?
Question by:PhilippeRenaud
    LVL 85

    Accepted Solution

    I've never played Starcraft not sure exactly how it is supposed to work.

    Here's an implementation that gives an average APM over the life of the app, and an "instantaneous" APM which only takes into account the last 60 seconds.  The values are updated every second:
    Public Class Form1
        Private WithEvents apmMonitor As New ActionPerMinuteMonitor
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        End Sub
        Private Sub apmMonitor_APM(apmAverage As Double, apmInstantaneous As Double) Handles apmMonitor.APM
            Label1.Text = apmAverage.ToString("0") & " avg / " & apmInstantaneous.ToString("0") & " inst"
        End Sub
    End Class
    Public Class ActionPerMinuteMonitor
        Implements IMessageFilter
        Public Sub New()
            TMR.Interval = TimeSpan.FromSeconds(1).TotalMilliseconds
        End Sub
        Private Const WM_KEYDOWN As Integer = &H100
        Private Const WM_LEFTMOUSEDOWN As Integer = &H201
        Private Const WM_RIGHTMOUSEDOWN As Integer = &H203
        Private Const WM_MIDDLEMOUSEDOWN As Integer = &H207
        Private SW As New Stopwatch
        Private ActionCounter As Long
        Private ActionStampsInLastMinute As New List(Of DateTime)
        Private WithEvents TMR As New System.Windows.Forms.Timer
        Public Event APM(ByVal apmAverage As Double, ByVal apmInstantaneous As Double)
        Private MillisecondsPerMinute As Integer = TimeSpan.FromMinutes(1).TotalMilliseconds
        Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
            Select Case m.Msg
                    ActionCounter = ActionCounter + 1
            End Select
            Return False ' Do NOT suppress any messages, allowing passage of all messages and thus normal operation of the application
        End Function
        Private Sub TMR_Tick(sender As Object, e As System.EventArgs) Handles TMR.Tick
            Dim apmAvg As Double = 0
            Dim apmInst As Double = 0
            If ActionCounter > 0 Then
                Dim mills As Long = SW.ElapsedMilliseconds
                apmAvg = CDbl(ActionCounter) / CDbl(mills) * MillisecondsPerMinute
                If ActionStampsInLastMinute.Count > 0 Then
                    Dim OneMinuteAgo As DateTime = DateTime.Now.AddMinutes(-1)
                    While ActionStampsInLastMinute.Count > 0 AndAlso ActionStampsInLastMinute(0) < OneMinuteAgo
                    End While
                    If mills < MillisecondsPerMinute Then
                        apmInst = apmAvg
                        apmInst = ActionStampsInLastMinute.Count
                    End If
                End If
            End If
            RaiseEvent APM(apmAvg, apmInst)
        End Sub
    End Class

    Open in new window

    LVL 1

    Author Comment

    Ok I tested, but not sure to understand 2 things:

    1) it seems but number are always the same ? that is for the first 60 secs ?

    2) if I minimize the app, seems it wont analyze the apm ? is it because the focus is not on the form ?
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    1) That's correct.  You can't have "per minute" metric if the first minute hasn't completely transpired yet.  Therefore for the first minute it uses the average metric.

    2) Also correct.  I'm using IMessageFilter which only intercepts messages intended for that application only.  If you need it for the entire system then we'll have to implement a low level keyboard hook and a low level mouse hook with WH_KEYBOARD_LL.  I can give you an example or provide links if that's the case.  *The question didn't really specify how this would be used so I went with the easiest implementation so it would be easier to understand.
    LVL 1

    Author Comment

    Idle_Mind, I would like a little modification, what If I want the APM when its Key press is "enter" on a specific textbox .. so apm would be only for a specific control ?

    Let me know when you read my comment so I can creat a new question and I will tell you the link.

    --- UPDATE

    I found it :)
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Sorry the delay Philippe...glad you found an answer!  =)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video discusses moving either the default database or any database to a new volume.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now