Click here to Skip to main content
12,348,925 members (58,009 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB10.0 VB.NET
Hey guys, I have a GUI in VB10 that's controlling some relays (cycling between 4 or sometimes 8 of them) and I'm using several timers to set delays between each 'switch' action.
I want to somehow throw in another independent timer (ideally display the time in a textbox) to keep track of how long the program takes to cycle through 4 relays (so that I have an idea of how long it takes to repeat this 1000 times). I want to make it start counting when the 'cycling/switching' action begins and stop counting when the action is done.
This time duration would include the known delays that I set with the timers that I'm using, but also include the time the GUI takes to execute the lines of code, which is unknown to me.


Can someone tell me how to implement the independent timer, maybe in the same way I use other timers? or I need to use multi-threading(which I'm not familiar with)?
THX
Posted 27-Dec-12 9:59am
Edited 27-Dec-12 10:00am
v2
Comments
Sergey Alexandrovich Kryukov 27-Dec-12 16:03pm
   
Why, why?!
—SA
SandiegoSSD 27-Dec-12 16:04pm
   
well.. a question not very well thought of I guess.
Now that I typed it out I think I should just use another timer
Sergey Alexandrovich Kryukov 27-Dec-12 16:10pm
   
Please check my answer first. If you need something else, please let me know, ok?
—SA
Zoltán Zörgő 27-Dec-12 16:10pm
   
First of all, what is the timescale you want to measure? Since Windows provides several timers. But PC's are not real-time systems, nor is Windows. And as he CPUs in PCs are CISC processors, not RISC ones, and windows is multitasking and multithreaded, execution time for the same code and same parameters is not always the same. So you can't use your windows PC as a high speed PLC. But, of course, if the constraints are acceptable, it can be used as controller in most situations.
Sergey Alexandrovich Kryukov 27-Dec-12 16:10pm
   
Yes, good point.
—SA
Zoltán Zörgő 27-Dec-12 16:13pm
   
Thank you.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

No, to time a procedure execution time, you don't need a timer at all. Instead, you need a stopwatch: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

Also, correct timing needs some understanding of how JIT compilation works: http://en.wikipedia.org/wiki/Just-in-time_compilation[^].

You need to exclude JIT time form execution time. If you time some method which was never called, or uses types or methods that were not yet used during run time, some considerable amount of time will be spend for JIT compilation. You will need to avoid such fresh calls. This is fairly easy though: just time some fragment of code only when it is repeated second time.

Good luck,
—SA
  Permalink  
Comments
Adam R Harris 27-Dec-12 16:18pm
   
Good answers, i just answered the specific question (How To Add A Timer) you went the extra mile and explained what he *should* be doing. I tip my hat to you kind sir.

+2 interwebs for you
Sergey Alexandrovich Kryukov 27-Dec-12 17:46pm
   
Oh, thank you very much for your nice words, Adam.
—SA

P.S.:
Interwebs? Excuse me, what do you mean?
Adam R Harris 27-Dec-12 17:56pm
   
Oh it's just a euphemism for the internet.
Sergey Alexandrovich Kryukov 27-Dec-12 18:13pm
   
+2 Internets? OK, whatever...
By the way, the use of the word "euphemism" suggests "Internet" is an offensive or suggestive term... :-)
Thank you,
—SA
SandiegoSSD 27-Dec-12 17:38pm
   
Thank you for the answer. Stopwatch is working for me. My timescale's pretty big. I just tested with four relays and it takes about 2 seconds to cycle through them with my code. Run it 10 times, each time is around several tens of milliseconds different from each other(I'm interfacing with mechanical arms and relays so it's not digital precise)
Sergey Alexandrovich Kryukov 27-Dec-12 17:45pm
   
Sure it will work for you: the duration data type used by this class is System.TimeSpan, it allows for huge scale of duration values.
Good luck, call again.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Put a new timer on your form, set the interval to 1000 (1 second). Them on the Elapsed (or Tick i can never remember) event update your textbox. When your process starts call Start on the Timer and when its done call Stop on your timer.

*** EDIT *****
Oh yea, if you are running long processes you should take a look at using the BackgroundWorkerProcess so your main thread doesn't get locked up.
http://www.dotnetperls.com/backgroundworker-vbnet[^]
  Permalink  
v2
Comments
Adam R Harris 27-Dec-12 16:17pm
   
Sergey is absolutely right - i answered the specific question your asked, Sergey told you what you should be doing instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100