473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

PerformanceCounter and ThreadID

Good morning,

Here is the objective: monitoring the CPU usage of a given thread in my
own C# application.

Here is what I do:
- on a Form I put a PerformanceCounter
- I set e CategoryName = Thread
- I set the CounterName = % Processor time
- I set the Machine = . (local machine)

Then I am trying to set the InstanceName and it is where it all goes
wrong...
The InstanceName should be something like: MyAppName/5

And the problem is I do not know how to get the right number (5 or 6 or
7 ???).

I have tried with :
- AppDomain.GetCurrentThreadId()
- System.Threading.Thread.CurrentThread.GetHashCode

But neither of them is the number I need for the PerformanceCounter.

Can you help please?

Thanks.

--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/
Apr 18 '06 #1
2 2739
CLR uses managed threads that do not have one to one mapping with OS
threads:

"An operating-system ThreadId has no fixed relationship to a managed thread,
because an unmanaged host can control the relationship between managed and
unmanaged threads. Specifically, a sophisticated host can use the CLR
Hosting API to schedule many managed threads against the same operating
system thread, or to move a managed thread between different operating
system threads."

You *could* use Win32 API to get OS thread but it's not fully stable.

Laura

"Michael Moreno" <mi*********************@free.fr> ha scritto nel messaggio
news:mn***********************@free.fr...
Good morning,

Here is the objective: monitoring the CPU usage of a given thread in my
own C# application.

Here is what I do:
- on a Form I put a PerformanceCounter
- I set e CategoryName = Thread
- I set the CounterName = % Processor time
- I set the Machine = . (local machine)

Then I am trying to set the InstanceName and it is where it all goes
wrong...
The InstanceName should be something like: MyAppName/5

And the problem is I do not know how to get the right number (5 or 6 or 7
???).

I have tried with :
- AppDomain.GetCurrentThreadId()
- System.Threading.Thread.CurrentThread.GetHashCode

But neither of them is the number I need for the PerformanceCounter.

Can you help please?

Thanks.

--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/

Apr 18 '06 #2

"Michael Moreno" <mi*********************@free.fr> wrote in message
news:mn***********************@free.fr...
| Good morning,
|
| Here is the objective: monitoring the CPU usage of a given thread in my
| own C# application.
|
| Here is what I do:
| - on a Form I put a PerformanceCounter
| - I set e CategoryName = Thread
| - I set the CounterName = % Processor time
| - I set the Machine = . (local machine)
|
| Then I am trying to set the InstanceName and it is where it all goes
| wrong...
| The InstanceName should be something like: MyAppName/5
|
| And the problem is I do not know how to get the right number (5 or 6 or
| 7 ???).
|
| I have tried with :
| - AppDomain.GetCurrentThreadId()
| - System.Threading.Thread.CurrentThread.GetHashCode
|
| But neither of them is the number I need for the PerformanceCounter.
|
| Can you help please?
|
| Thanks.
|
| --
| Michael
| ----
| http://michael.moreno.free.fr/
| http://port.cogolin.free.fr/
|
|

'GetCurrentThreadId' returns the OS thread ID, which is the same as
perfcounter "ID Thread" counter, so you will have to compare this Perf
counter with the value returned by GetCurrentThreadId to find the right
thread.
Note that:
- GetCurrentThreadId is deprecated in V2 of the framework, you better
PInvoke Win32's GetCurrentThreadId API.
- you might consider using a separate process to monitor a process thread
consumption, monitoring your own process thread consumption is quite
intrusive and may disturb normal thread scheduling.
I posted a sample of the latter to your previous thread "Thread-CPU usage"
in this NG.

Willy.
Apr 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: W1ld0ne74 | last post by:
I am implimenting Performance counters into a web application. I use the following code to create the counters during setup: private void SetupPerfCntrs() {...
1
by: Chris | last post by:
I'm trying to do something really easy: just get the % CPU load. However, I just get mostly 0's sprinkled with an occasional 100. I poked around on the web for info, but all the stuff I found (I...
1
by: jimbo | last post by:
Here is my problem. I'm creating an Instrumentation class that will use previously created Performance Categories and Counters in order to time various processes (ie. query duration etc.). This...
0
by: BuddyWork | last post by:
Hello, Can someone please explain why my Instances are not appearing in Perfmon. Here is my code to create the counters. CounterCreationDataCollection CCDC = new...
0
by: Michiel | last post by:
Hello, I'm trying to retrieve my cpu load as follows : PerformanceCounter* cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); float value =...
3
by: Rob Meade | last post by:
Hi all, I'm having a bit of trouble with the following function.... Private Function GetSystemUpTime() As TimeSpan ' declare variables Dim Result As TimeSpan Dim PerformanceCounter As...
0
by: Scheu | last post by:
Hi all, I'm hoping someone can help me with this bizarre problem. If I create a new PerformanceCounter object in a ComVisible class and invoke it from wscript, it never seems to terminate. When...
3
by: Geoff McElhanon | last post by:
I have been struggling with a security issue that occurs under .NET 2.0, but does not occur under .NET 1.1. Essentially I am trying to open up a performance counter on a remote server and monitor...
1
by: bobido | last post by:
Hi there! Currently I'm trying to display the current performance of my servers on my website. I tried it with the following code: protected static PerformanceCounter cpuCounter; ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.