473,396 Members | 2,009 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,396 software developers and data experts.

Quantity of CPU time used by a thread

Is there a way to measure or ask .NET to tell me what amount of time a
thread spent actually executing on the CPU?

For example, I may spawn a thread that spends 99.9% of its time
waiting for an event to be set, and then performs some action. If the
thread runs for 10 hours, how can I find out what amount of that time
was spent doing real execution.

For a simple example, I could measure this myself... For example, if I
just had a loop that waited for an event to be set I could put a high
performance timer everytime I got into the body of the loop and
increment a total time before going back into my wait mode.

However, if my code in turn calls lots of other functions which may
also block waiting on some asynchronous action this gets really
complicated and kinda infeasible if the other code is out of my
control.

Therefore, I was hoping for some magic API call that gives me this
info.

Any ideas?

Thanks,
-ken
Nov 15 '05 #1
3 5370
The only highly accurate way is to use GetThreadTimes (a Win32 API call). I
seem to recall having to do a GetCurrentThread, DuplicateHandle on this, and
finally CloseHandle when you are done. It isn't nice. The other option is
to use the profiler interfaces, but I don't think they have a managed
equivalent for those either.

--
Justin Rogers
DigiTec Web Consultants, LLC.

"Ken Durden" <cr*************@hotmail.com> wrote in message
news:18**************************@posting.google.c om...
Is there a way to measure or ask .NET to tell me what amount of time a
thread spent actually executing on the CPU?

For example, I may spawn a thread that spends 99.9% of its time
waiting for an event to be set, and then performs some action. If the
thread runs for 10 hours, how can I find out what amount of that time
was spent doing real execution.

For a simple example, I could measure this myself... For example, if I
just had a loop that waited for an event to be set I could put a high
performance timer everytime I got into the body of the loop and
increment a total time before going back into my wait mode.

However, if my code in turn calls lots of other functions which may
also block waiting on some asynchronous action this gets really
complicated and kinda infeasible if the other code is out of my
control.

Therefore, I was hoping for some magic API call that gives me this
info.

Any ideas?

Thanks,
-ken

Nov 15 '05 #2
You could continually check the System.Environment.TickCount (think thats
it). It might not be accurate though, because TickCount measures in
milliseconds. if all you're doing is checking a single variable to see if
it's true or not, it will likely take under 1 ms, and simply adding up the
results of all of the cycles in the loop would still leave you with 0. What
I do to make sure that my loops don't steal too much cpu time is make them
go to sleep for a second or 2 during a loop, especially if they're not
needed to react instantly to a change.

....
while(true) {
if(breakTheLoop) break;
System.Threading.Thread.Sleep(2000); // sleep for 2 seconds
}
....
"Ken Durden" <cr*************@hotmail.com> wrote in message
news:18**************************@posting.google.c om...
Is there a way to measure or ask .NET to tell me what amount of time a
thread spent actually executing on the CPU?

For example, I may spawn a thread that spends 99.9% of its time
waiting for an event to be set, and then performs some action. If the
thread runs for 10 hours, how can I find out what amount of that time
was spent doing real execution.

For a simple example, I could measure this myself... For example, if I
just had a loop that waited for an event to be set I could put a high
performance timer everytime I got into the body of the loop and
increment a total time before going back into my wait mode.

However, if my code in turn calls lots of other functions which may
also block waiting on some asynchronous action this gets really
complicated and kinda infeasible if the other code is out of my
control.

Therefore, I was hoping for some magic API call that gives me this
info.

Any ideas?

Thanks,
-ken

Nov 15 '05 #3
Justin,

If I can figure out all the interop stuff to use this in MC++ then it
should be perfect... Thanks!

-ken

"Justin Rogers" <Ju****@games4dotnet.com> wrote in message news:<#g**************@TK2MSFTNGP12.phx.gbl>...
The only highly accurate way is to use GetThreadTimes (a Win32 API call). I
seem to recall having to do a GetCurrentThread, DuplicateHandle on this, and
finally CloseHandle when you are done. It isn't nice. The other option is
to use the profiler interfaces, but I don't think they have a managed
equivalent for those either.

--
Justin Rogers
DigiTec Web Consultants, LLC.

Nov 15 '05 #4

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

Similar topics

1
by: Michael J. Astrauskas | last post by:
I'm developing a fairly basic shopping cart. My original implementation had the user click on a link to add an item to the cart (addtocart.php?item=123456). I figured it could just increase the...
77
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
3
by: Terje | last post by:
Hello I have a function like this function checkquantitiy(quantitiy){ if(quantitiy.value != parseInt(quantitiy.value)) { alert(quantitiy.value+" sorry not integer quantitiy"); } } ...
2
by: Gary Vidal | last post by:
I have a shopping cart webpage that shows a product and the Quantity they would like to order: When you click the link I want to take the quantity that they entered in the textbox and post that to...
5
by: Jeremy | last post by:
I have a core VB service that monitors a database, and based on data in the records will execute code to send email notifications. Problem: I don't want my main program code to halt and wait for...
5
by: HandersonVA | last post by:
should I set to "0" as a default value on a quantity and a price field or set to "null"? if a user enter nothing on the quantity or price field on a web browser, should I treat it as null or "0"?...
9
by: Rob Meade | last post by:
Hi all, Ok - so I've got the array thing going on, and the session thing going on, and up until now its all been ok. I've got my view basket page which is displaying 3 rows (as an example) - I...
14
by: kang jia | last post by:
hi i am doing shopping online, i will let user choose their prodcut in the first page and then when they click" order" button, they will be redirected to do_addcart.php. i will insert their orders...
5
by: goldstar | last post by:
hello all. Another problem, since i have joined all i have been doing is asking for help. i need to repay all these favours!!! I currently have an order form, and a product form, Scenario: if...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.