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

x
?
Solved

APM (Action per minute) application vb.Net

Posted on 2012-09-18
5
Medium Priority
?
643 Views
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 vb.net ?
:)
0
Comment
Question by:PhilippeRenaud
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 38410813
I've never played Starcraft 2...so 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
        Application.AddMessageFilter(apmMonitor)
    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()
        SW.Start()
        TMR.Interval = TimeSpan.FromSeconds(1).TotalMilliseconds
        TMR.Start()
    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
            Case WM_KEYDOWN, WM_LEFTMOUSEDOWN, WM_RIGHTMOUSEDOWN, WM_MIDDLEMOUSEDOWN
                ActionCounter = ActionCounter + 1
                ActionStampsInLastMinute.Add(DateTime.Now)

        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
                    ActionStampsInLastMinute.RemoveAt(0)
                End While

                If mills < MillisecondsPerMinute Then
                    apmInst = apmAvg
                Else
                    apmInst = ActionStampsInLastMinute.Count
                End If
            End If
        End If

        RaiseEvent APM(apmAvg, apmInst)
    End Sub

End Class

Open in new window

0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 38411398
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 ?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38411414
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.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 39124338
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 :)
0
 
LVL 86

Expert Comment

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

872 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