473,624 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PerformanceCoun ter 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 PerformanceCoun ter
- 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.GetCu rrentThreadId()
- System.Threadin g.Thread.Curren tThread.GetHash Code

But neither of them is the number I need for the PerformanceCoun ter.

Can you help please?

Thanks.

--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/
Apr 18 '06 #1
2 2760
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 PerformanceCoun ter
- 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.GetCu rrentThreadId()
- System.Threadin g.Thread.Curren tThread.GetHash Code

But neither of them is the number I need for the PerformanceCoun ter.

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 PerformanceCoun ter
| - 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.GetCu rrentThreadId()
| - System.Threadin g.Thread.Curren tThread.GetHash Code
|
| But neither of them is the number I need for the PerformanceCoun ter.
|
| Can you help please?
|
| Thanks.
|
| --
| Michael
| ----
| http://michael.moreno.free.fr/
| http://port.cogolin.free.fr/
|
|

'GetCurrentThre adId' 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 GetCurrentThrea dId to find the right
thread.
Note that:
- GetCurrentThrea dId is deprecated in V2 of the framework, you better
PInvoke Win32's GetCurrentThrea dId 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
3290
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() { System.Diagnostics.CounterCreationDataCollection CounterDatas = null; System.Diagnostics.CounterCreationData cdCounter4 = null; try { if(System.Diagnostics.PerformanceCounterCategory.Exists("LogisOnline"))
1
7271
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 think) does what I have below. Am I blanking out on something here? Getting the available RAM works fine. Code example below: using System;
1
4218
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 Instrumentation class will be used by a variety of "services", so the Categories and Counters to be used within the object must be set during object construction. So I created a class variable array of: private static PerformanceCounter...
0
1277
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 CounterCreationDataCollection();
0
1100
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 = cpuCounter->NextValue(); However, this always returns 0.0. The same query used in perfmon.exe works fine.
3
2880
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 PerformanceCounter
0
1233
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 debugging, Visual Studio never leaves the (running) state when the Go function exits (even clicking the 'Stop Debugging' button fails). When not debugging, the cscript / wscript.exe process never leaves memory. I've tried this same code in a...
3
4584
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 its value. In .NET 1.1 this worked fine, however under .NET 2.0 it fails when I am not an administrator on the remote server. To provide a lean demonstration of the issue, I created the following class: ============================
1
4190
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; protected static PerformanceCounter ramCounter; protected void Page_Load(object sender, EventArgs e) {
0
8249
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4187
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2613
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1797
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1493
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.