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.)
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.
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
Re: Option strict late binding (CPU clock speed prj)
Quote:
Originally Posted by
Shaggy Hiker
then you can use TryCast,
Can you hint a clue, how to do that?
Quote:
Originally Posted by
dbasnett
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.
Re: Option strict late binding (CPU clock speed prj)
Quote:
Originally Posted by
pourkascheff
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?
Re: Option strict late binding (CPU clock speed prj)
Quote:
Originally Posted by
pourkascheff
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?
Re: Option strict late binding (CPU clock speed prj)
Quote:
Originally Posted by
jmcilhinney
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?
Re: Option strict late binding (CPU clock speed prj)
Quote:
Originally Posted by
pourkascheff
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.
1 Attachment(s)
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.
Attachment 187363
Re: Option strict late binding (CPU clock speed prj)
Quote:
Originally Posted by
jmcilhinney
Did you import the required namespace, as shown in the code provided?
Yes.
Quote:
Originally Posted by
jmcilhinney
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
those types
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
Re: Option strict late binding (CPU clock speed prj)
Quote:
Originally Posted by
jmcilhinney
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:
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"))
Label5.Text = name.ToString & "(" & speed.ToString & "MHz)"
Next
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
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
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).
Re: Option strict late binding (CPU clock speed prj)
Quote:
Originally Posted by
pourkascheff
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.