Results 1 to 13 of 13

Thread: Option strict late binding (CPU clock speed prj)

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Option strict late binding (CPU clock speed prj)

    Consider following code works on Option Strict = off:
    Code:
    Dim MyOBJ As Object
    Dim cpu As Object
    MyOBJ = GetObject("WinMgmts:").instancesof("Win32_Processor")
    For Each cpu In MyOBJ
                MsgBox(cpu.Name.ToString + " " + cpu.CurrentClockSpeed.ToString + " Mhz")
    Next
    How can we use it without late biding error? Plus the original idea has a "Set" term before "MyOBJ = GetObject(..." part which VS automatically removes it.

    Notes:
    - In my case it returns 2511MHz in clock frequency (at a loop to a label) all the time which regarding to CPUz small toolkit I'm monitoring changeable value beyond 3300MHz. Why is it constant?
    - I rather to use "Performance Counter" component and for following properties,
    Code:
    .CategoryName = "Processor Performance"
    .CounterName  = "Processor Frequency"
    .InstantName  = "PPM_Processor_0" 'to _3 were my options and already tested them all
    .MachineName  = "."
    It also returns 2511 and still a fixed value.

    I would be appreciated if anyone test the Performance counter way on his/her console and give a feedback (I only tested on laptops.)

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Option strict late binding (CPU clock speed prj)

    To stop getting the late binding error, stop using late binding. Late binding is where you write code that doesn't specify the type and just assume that when it comes to runtime, the correct type of object will be found. In this case, you are declaring things of type Object, but then you are using them as some other type. If you KNOW that they are some other type, then DirectCast() can be used to turn them into the right type. If they aren't that type, then DirectCast will crash at runtime (and so will late binding, because it's not magic). Therefore, if you aren't SURE what type the object will actually be, then you can use TryCast, but then you'll have to be sure to check the return to see whether or not the cast was successful.
    My usual boring signature: Nothing

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Option strict late binding (CPU clock speed prj)

    With Option Strict ON

    Code:
    Imports System.Management
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            test()
        End Sub
    
        Public Sub test()
            Try
                Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_Processor")
    
                For Each queryObj As ManagementObject In searcher.Get()
                    Dim name As String = queryObj("Name").ToString
                    Dim speed As Double = CDbl(queryObj("CurrentClockSpeed"))
                    Debug.WriteLine("-----------------------------------")
                    Debug.Write("Name: ")
                    Debug.Write(name)
                    Debug.Write("  CurrentClockSpeed: ")
                    Debug.Write(speed)
                    Debug.WriteLine(" MHz")
                Next
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Sub
    End Class
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: Option strict late binding (CPU clock speed prj)

    Quote Originally Posted by Shaggy Hiker View Post
    then you can use TryCast,
    Can you hint a clue, how to do that?

    Quote Originally Posted by dbasnett View Post
    With Option Strict ON
    Code:
    Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_Processor")
    Is it WinForm code? I'm afraid my VS12 does not recognize . Error: Type 'ManagementObjectSearcher' is not defined. Plus catch statement err was as exception before. Here VS12 doesn't recognize "ManagementException" neither.

    - Is it cover all mainboards and all kind of processors?
    - You people ain't believe in Performance counters, are you? =))) C'mon let's stick to built in tools.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Option strict late binding (CPU clock speed prj)

    Quote Originally Posted by pourkascheff View Post
    Is it WinForm code? I'm afraid my VS12 does not recognize . Error: Type 'ManagementObjectSearcher' is not defined. Plus catch statement err was as exception before. Here VS12 doesn't recognize "ManagementException" neither.
    Did you import the required namespace, as shown in the code provided? Did you reference the required assembly, which you can see at the top of the documentation for those types that you should have already read for yourself?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Option strict late binding (CPU clock speed prj)

    Quote Originally Posted by pourkascheff View Post
    Can you hint a clue, how to do that?
    What do you not understand about the information that you found when you looked for yourself? Did you bother to read the relevant documentation? Did you bother to look for existi8ng examples already on the web?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: Option strict late binding (CPU clock speed prj)

    Quote Originally Posted by jmcilhinney View Post
    Did you bother to read the relevant documentation?
    I believe in instruction manuals and documentations. I grew up this way plus I used to work in avionics so by default assume your audience might be familiar with it. This is the matter of time. I still remember you taught me DirecCast dictation next to a practical example, thank you for that. Why you're mean to me?

    Long story short, I'm taking some distant from that wmi/object way intentionally, because it let me down few times before, also it's sensibly slow and in final using Performance counters were already taken place in this project simultaneously so why not sticking to a pattern-way of coding?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Option strict late binding (CPU clock speed prj)

    Quote Originally Posted by pourkascheff View Post
    Why you're mean to me?
    How is it mean to ask you whether you have read the documentation? I'm not going to sugar coat it - RTFM is a cliche for a reason - but it seems to me that people only think that it's mean to do that because they know to not do it is wrong, and they don't think that people should point out that they're doing the wrong thing. If I was calling you names because you didn't read the documentation, that could be considered mean. Asking you whether you've done something that you should have done and should know you should have done is not being mean.
    Last edited by jmcilhinney; Apr 7th, 2023 at 07:05 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: Option strict late binding (CPU clock speed prj)

    Update
    Consider task manager's "speed" indicator versus "Base speed". I think Performance counter gets the latter.

    Additional content (light blue circle): Did you know that Microsoft Windows 11 (and maybe 10, didn't pay attention) won't turn off your computer by shutdown command? It performs a sort of hibernate. So my console wasn't still ON for 33 days.
    Name:  cpuspeedtaskmanager.jpg
Views: 478
Size:  42.3 KB

  10. #10

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: Option strict late binding (CPU clock speed prj)

    Quote Originally Posted by jmcilhinney View Post
    Did you import the required namespace, as shown in the code provided?
    Yes.
    Quote Originally Posted by jmcilhinney View Post
    Did you reference the required assembly, which you can see at the top of the documentation for those types that you should have already read for yourself?
    I'm sorry, I am not sure what is it. I only know "assembly information" in assemblies. What do I need to lookup to know that? I'm not familiar with
    Quote Originally Posted by jmcilhinney View Post
    those types

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Option strict late binding (CPU clock speed prj)

    An assembly is a .NET executable, i.e. DLL or EXE. Read the documentation for the types you want to use and you'll see what assembly they are declared in. If your project doesn't have a reference to that assembly, add one. Importing a namespace is useless if your project doesn't know that types in that namespace exist. You should read this:

    http://jmcilhinney.blogspot.com/2009...importing.html
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: Option strict late binding (CPU clock speed prj)

    Although it covers things which make you do wonder-face multiple times, But I'm afraid it didn't help me at all @jmcilhinney.

    I added it manually and it worked through following address: Project settings, References tab, If you typed "Imports System.Management" but it isn't shown in list, find the this namespace in checklistbox below. In final press "Add" button in middle of the page (Reference... not Service reference...) also mark management as checked then OK.

    Now the @dbasnett's code (which were turned into):
    vb Code:
    1. Try
    2.             Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_Processor")
    3.             For Each queryObj As ManagementObject In searcher.Get()
    4.                 Dim name As String = queryObj("Name").ToString
    5.                 Dim speed As Double = CDbl(queryObj("CurrentClockSpeed"))
    6.                 Label5.Text = name.ToString & "(" & speed.ToString & "MHz)"
    7.             Next
    8.         Catch err As ManagementException
    9.             MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
    10.         End Try
    Works. But it still return fixed base clock speed. We were able to get it from Performance Counters...

    Quote Originally Posted by pourkascheff View Post
    Name:  cpuspeedtaskmanager.jpg
Views: 478
Size:  42.3 KB
    To be more specific I already achieved lots of task manager's labels value sources. For instance in CategoryName of "Process" and/or "Process V2"
    Code:
    .CounterName  = "Handle Count"
    .InstantName  = "_Total"
    .MachineName  = "."
    Returns processor #handles (57140 in screenshot) and
    Code:
    .CounterName  = "Thread Count"
    .InstantName  = "_Total"
    .MachineName  = "."
    Returns processor #threads (1305 in screenshot).

    How can I access that 2.92GHz (in screenshot) which varies from that fixed based frequency (which is the minimum) to the maximum and overclock limit?

    Notes:
    Performance Counter have both "Object" and "WMI Object" as categories but their instances are empty (in my case).

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Option strict late binding (CPU clock speed prj)

    Quote Originally Posted by pourkascheff View Post
    Although it covers things which make you do wonder-face multiple times, But I'm afraid it didn't help me at all @jmcilhinney.
    That is truly incredible to me and a sign that I'm wasting my time here, so I'm out.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width