 |
|
 |
i want a timer c# code in which i call a method which is dynamic..and which is to be exicuted every 30 mints
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
Very nice. But I have a question. I would like to create an Event that triggers every so many milliseconds. I only need 1ms accuracy at the most. Now I’ve tried the easy way with the Clock method. But not matter what I try for the Interval prop, the results are not reliable (1ms Interval gets me 14,15,16ms between the printed time in my log file - 20ms gives me 30,31,32ms). Your methods is more accurate but only used as a stopwatch. Is there a way to generate an Event using this method? So I can do something simple (write a line to a log file) every so many ms?
Thanks!
Marco
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
you might add a frame timer. Depending on the business, it is called a continuous timer or sample timer, or a time marker. The concept is basically that your end time from one event is the start of another. Although you can add multiple start/stop and duration calls, the extra start command has very small time lost from the stop to the restart.
In continuous mode the stop time is moved to the start after evaluating a marked time duration. Or alternately the previous stop-time is moved to the start prior to calling the stop time. Very handy in evaluating time on continuous events.
I added my own, but thought you might consider it. Thanks for the article!
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb) John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others."
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi you! In .NET,garbage colection is implemented automatically.It can cause your class operating incorrect. I think you should insert in your class as follow: public void Start() { // lets do the waiting threads there work Thread.Sleep(0); GC.Collect();// GC.WaitForPendingFinalizers();// QueryPerformanceCounter(out startTime); } is it ok? Best Regards
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
.NET 2.0 now provides this functionality as part of the framework. See class: System.Diagnostics.Stopwatch in System.dll.
Kind Regards, Mark
|
| Sign In·View Thread·PermaLink | 4.58/5 (5 votes) |
|
|
|
 |
|
 |
Nice clean code, unlike a lot of the samples you get on Code Project. Two small suggestions:
1) Don't initialize the Int64's to 0, the runtime does that for you. 2) Flag the two native methods with the following attribute: [return: MarshalAs(UnmanagedType.Bool)]
This brings the code more into line with the CLR specifications, and runs past FxCop without problems.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
Hi,
There a couple of gotcha's you have to watch out for when using Query... API.
First, and most important is that on some platforms (namely mobile platforms, but also some servers too), the frequecy can change dynamically during executation of your code. If this happens, then you'll need to take this into account in more complicated way.
Second, is the question of Multi-core. Your thread may begin timing on one core and then finish on another core. If you are using the classic RDTSC method (Read TimeStamp) then you can be in big trouble because the clocks between cores are not synchronized and definitely start at different times.
QueryPerformanceCounter is supposed to take this into account. However if you read the fine print in MSDN, there seems to suggest that this method is not guaranteed either.
In general, when I'm trying to get precise measurements, I will use SetThreadAffinity (don't remember the C# equivalent off top of my head) to tie the thread to a single core and then measure. If you have multiple threads involved in the part you are timing, then well it gets more complicated....
Aaron
|
| Sign In·View Thread·PermaLink | 4.25/5 (3 votes) |
|
|
|
 |
|
 |
Hi,
I like your article, but I wanted to point out a problem that will help you make it even better. There is no such thing as an exact measurement of time. Exactness or accuracy of a measurement is the degree of conformity with a standard (the "truth"). The difference between the measurement and the standard represents the error. Precision relates to the consistency of which a measurement may be reproduced and to the number of significant digits a measurement may reliably be made. Just because you can reliably measure something out to 10 decimal places doesn't mean that it is accurate (or exact) out to 10 decimal places. So, it is more correct to say that your application provides a more precise measurement of time.
Gary Kirkham
Forever Forgiven and Alive in the Spirit
He is no fool who gives what he cannot keep to gain what he cannot lose. - Jim Elliot
Me blog, You read
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Gary, Could you please point out in this context how is the QueryPerformanceCounter not "ACCURATE" and how to remedy this problem!!!
Thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
get the occasional 0 or 0.0000000000000000001 but that's OK, timing is tricky. Thanks for the class, saved me some time )
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I was initially puzzled by the following code -- especially the comment!
// Start the timer public void Start() { // lets do the waiting threads there work Thread.Sleep(0); QueryPerformanceCounter(out startTime); } It seems to me that the real reason to call Sleep(0) here is simply to resume this thread's execution in a fresh time-slice, to reduce the probability it will be suspended during the measurement. (Of course, the scheduler may have different ideas!) So let me suggest the following clarification:
// Start the timer public void Start() { // abandon any remaining time in this slice Thread.Sleep(0);
// resume in a fresh time slice (if you please, Mr. Scheduler!) QueryPerformanceCounter(out startTime); } Nevertheless, the Sleep(0) call is a reasonable thing to do there. One could also increase the thread's .Priority to (perhaps) further ward off a premature suspension. Again, the Windows scheduler may not cooperate.
Performance measurement is always a trick, without hardware assist!
DrGary
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Dear Sir, Iam a new progrmmer and I need this code to measure time for some process with high precision, and iam using Visual C++ 6, and when i copy this code and paste it in the Visual C++ and compile the code it gives errors which iam pasting now: Can you please tell me which mistake am i making thanks
Compiling... Text1.c D:\USER\Perkash\Text1.c(1) : error C2061: syntax error : identifier 'System' D:\USER\Perkash\Text1.c(1) : error C2059: syntax error : ';' D:\USER\Perkash\Text1.c(2) : error C2061: syntax error : identifier 'System' D:\USER\Perkash\Text1.c(2) : error C2059: syntax error : ';' D:\USER\Perkash\Text1.c(2) : error C2059: syntax error : '.' D:\USER\Perkash\Text1.c(3) : error C2061: syntax error : identifier 'System' D:\USER\Perkash\Text1.c(3) : error C2059: syntax error : ';' D:\USER\Perkash\Text1.c(3) : error C2059: syntax error : '.' D:\USER\Perkash\Text1.c(4) : error C2061: syntax error : identifier 'System' D:\USER\Perkash\Text1.c(4) : error C2059: syntax error : ';' D:\USER\Perkash\Text1.c(4) : error C2059: syntax error : '.' D:\USER\Perkash\Text1.c(6) : error C2061: syntax error : identifier 'Win32' D:\USER\Perkash\Text1.c(6) : error C2059: syntax error : ';' D:\USER\Perkash\Text1.c(7) : error C2449: found '{' at file scope (missing function header?) D:\USER\Perkash\Text1.c(58) : error C2059: syntax error : '}' Error executing cl.exe.
Text1.exe - 15 error(s), 0 warning(s)
with regards Perkash
|
| Sign In·View Thread·PermaLink | 1.00/5 (3 votes) |
|
|
|
 |
|
 |
Probably too late to be helpfull. The problem you are having is that this example is written in C# and your trying to write it in C++. I do not know how you would make a similar functionality in C++, but I am sure it can be converted.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Instead of using this code, just tap into the API directly. You can't use this code. It's not the right programming language.
ROFLOLMFAO
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
The KB article is less accurate than the code provided in the article above.
If you use the KB code, you'll need to initialize the variables you pass to the API functions (by CLR verification rules), and what's more you'll be required to check for non-zero / zero for success / fail, while P/Invoke can do this automatically.
It's better to use the functions in the article above.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Anonymous wrote: The KB article is less accurate than the code provided in the article above.
I think it's better to cite some source and reference links when you attempt to contradict a Microsoft support article about how to call a Microsoft API function from the Microsoft .NET framework.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
Then that's probably entirely your fault, since you obviously didn't read the code:
'Returns the duration of the timer (in seconds)'
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I am calling the High-Performance Timer class methods repeatedly and receiving incorrect results. Has anyone else experienced inaccurate times.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
This is because this operating System is muti-threading and the time alloted to your thread is not consistent.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I'm also getting incorrect results. If you move the QueryPerformanceCounter calls into the code/function you are profiling, though, it seems to work just fine. The incorrect results seem to be due to the uneven time it takes to call the HiPrefTimer member functions (and are not a result of something running in the background, as the early reply suggests).
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
 |
When you use these timer functions, you need to make sure it processes in only ONE thread. Do this by setting the thread's affinity to the processor. Use this Win32 API call: SetThreadAffinityMask()
|
| Sign In·View Thread·PermaLink | 4.00/5 (2 votes) |
|
|
|
 |