VB.NET Conversion (MHz to GHz)

Discussion in 'Mixed Languages' started by QuantumBug, Apr 20, 2012.

  1. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,327
    60
    Having a problem creating a function to convert processor mhz to ghz. I've done Bytes/MB/GB, but this is just proving a pain in the backside.

    When I grab my i3 processor speed from WMI, the string reads 2133(mhz) which is plain ugly and would like it to show...

    (speed in ghz) x (cores), so essentially the string for my Laptop would read '2.13GHz x 4'

    Can anyone whip me up a conversion function, or am I going to have to figure it out myself :(?

    Thanks in advance, Dave.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,327
    60
    #2 QuantumBug, Apr 20, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Ok, I was being an idiot, this did it...

    Code:
    lbhertz.Text = (FormatNumber(Get_Clock_Speed) / 1000 / 1) & "GHz"
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,884
    20,225
    180
    #3 Alphawaves, Apr 20, 2012
    Last edited by a moderator: Apr 20, 2017
    Hi incase you haven't already done it..

    Does this work for you:
    Code:
    Public Function PSpeed()
            Dim cc As String = ""
            Dim result As String = ""
            Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_Processor")
            For Each Obj As ManagementObject In searcher.Get()
                cc = Obj("NumberOfCores")
                Dim speed As Double = CDbl(Obj("CurrentClockSpeed"))
                Select Case speed
                    Case Is >= 1000
                        result = String.Format("Processor: {0:0.00} GHz x " & cc, speed / 1000, 1)
                    Case Else
                        result = String.Format("Processor: {0:0.00} MHz x " & cc, speed, 1)
                End Select
            Next
            Return result
        End Function
    Usage:
    Code:
    CPUS.Text = PSpeed()
    ;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,488
    1,327
    60
    #4 QuantumBug, Apr 20, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    As usual, Alphawaves wins the thanks!

    Yes it works better as my way reports 2.133GHz and once modified yours reports 2.13...

    One annoying problem, when your object is 'CurrentClockSpeed' from Win32_Processor, if you place this in a Timer to refresh the clock speed it crashes the app on launch.

    Code:
    Private Sub ExecutePerc_Tick(sender As System.Object, e As System.EventArgs) Handles ExecutePerc.Tick
    
    lbhertz.Text.Refresh
    lbHertz.Text = PName()
    
    End Sub
    
    Do I have to invalidate it or something to stop it from crashing?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...