Page 1 of 2

Making a timer application.

Posted: 25 May 2007, 09:39
by Tander
Morning guys.

I am trying to make a timer application. Whereby I can input a timestamp into a form, press start and then, when I press stop, it will tell me how long it was till I pressed the stop. I want 8 of these, so I presume I just repeat the code 8 times for each timestamp, correct?

But, it's the actual code that I am unsure of on how to do. Anyone got any ideas on how to do this?

Thanks guys!

Posted: 25 May 2007, 09:46
by rustypup
platform?

language?

you're talking forms... can we presume some flavour of vb?

if all you're doing is tracking snapshots of the system clock... normally stored as a long integer, (64bits). you would take the initial snapshot when start is pressed and the comparative when stop is pressed... the difference between the two is the time elapsed...

you use a timer when you require some form of automation... ie, no user intervention...

if you require that the form output be updated between presses, then you need a seperate thread running which can handle that for you, leaving the execution thread free to trap events. how you do this depends on the platform/language being used...

Posted: 25 May 2007, 09:58
by HdB
Normally you should get the system time when you click the Start button, and again when you click the Stop button.

The compute the difference between the two. I THINK .NET can show the difference in miliseconds, or ticks, not quite sure.

Posted: 25 May 2007, 10:32
by Hex_Rated
In VB.NET

Code: Select all

Dim dtTimeStamp As DateTime

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dtTimeStamp = DateTime.Now() ' start
        MessageBox.Show(dtTimeStamp.ToString("hh:mm:ss"))
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MessageBox.Show(((DateTime.Now().Ticks() - dtTimeStamp.Ticks()) / 1.0E+7F).ToString() + "s") ' stop
    End Sub
Assuming you have two buttons on the form. First button stamps, second calculates.

Posted: 25 May 2007, 10:49
by HdB
@Hex_Rated:

Sorry for my ignorance, but what does this part do?
/ 1.0E+7F

Posted: 25 May 2007, 10:52
by Hex_Rated
Divide by 10,000,000 and convert to float. Will convert ticks to seconds. It would normally read:

Code: Select all

/10000000f
but the IDE changes it.

Posted: 25 May 2007, 10:53
by Tander
Yes, sorry VB is what I am using.

Hex_Rated: That seems like what I want. The only part I don't understand is what control/code do I use to declare the 'dtstamptime' ?

Can I repeat the code for 8 boxes ect?

Posted: 25 May 2007, 11:04
by Hex_Rated
Hex_Rated: That seems like what I want. The only part I don't understand is what control/code do I use to declare the 'dtstamptime' ?
You need to make it global so you can use it in separate functions. It needs to be defined outside of any subs or functions.
Can I repeat the code for 8 boxes ect?
You can make 8 separate vars, an array would work best but you could give them separate names as well if you aren't comfortable with arrays yet. You need a separate dtTimeStamp var for each timer. Use the same calculation just substitute the variable name for the one your trying to work out.

Posted: 25 May 2007, 11:10
by Tander
Hex_Rated wrote:
You need to make it global so you can use it in separate functions. It needs to be defined outside of any subs or functions.
Sorry, Hex_Rated, I am still fairly new to all this. Do you mean I need to make it in its own class, then what code goes there? Just a timer?

Posted: 25 May 2007, 11:16
by Hex_Rated
All you need to do is put

Code: Select all

Dim dtTimeStamp as DateTime
Anywhere in your form as long as it's not inside a subroutine. Let's say you double clicked a button on your form designer and it created this code for you:

Code: Select all

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
End Sub
You can put the dtTimeStamp declaration immediately above it:

Code: Select all

Dim dtTimeStamp as DateTime

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
End Sub
Now it will be global and accessible to any sub routine in that form.

Posted: 25 May 2007, 11:22
by Tander
Makes sense.

Thank you, will give it ago. Do I need text boxes on the form?

Posted: 25 May 2007, 11:26
by Hex_Rated
No. Create two buttons and put the code underneath their events. The IDE will create the sub routine definitions, just copy the code underneath the sub .. till end sub in each event handler. When you click the first button the time will be displayed, when you click the second button the difference in time will be displayed in seconds.

Posted: 25 May 2007, 11:45
by Tander
Thanks, Hex. Will try it out now. :D

Posted: 25 May 2007, 14:55
by PHR33K
Step one:
Learn a real language

Posted: 28 May 2007, 11:36
by Tander
PHR33K wrote:Step one:
Learn a real language
And that would be? :roll:

Posted: 28 May 2007, 14:46
by HdB
Usually, that means the same as "Anything without Microsoft in the title" :roll:

If you are comfortable with .NET, and it does everything you need, then use it.

By the way, what IDE are you using? Visual Studio? I like to use SharpDevelop. Its free and open source.

Posted: 29 May 2007, 20:43
by PHR33K
I'm pretty good at C# but in reality .NET sucks.
C/C++ is easily the way to go for sheer programming power (granted, commercially something like java is preferable but meh).
Then theres Assembler, I'm rather partial to both MASM32 and FASM.

Posted: 29 May 2007, 23:34
by Hex_Rated
C# isn't any more efficient than VB.NET. They both end up as byte code and are compiled to native code at run time by a dynamic compiler. C++ and assembler are far more efficient but you would be mad to use them to write an application where you need low development overhead or where performance isn't critical.

Posted: 30 May 2007, 07:12
by PHR33K
Yeah, C# and VB.NET are in the end the same thing, but god, C# is worlds more comfortable to use.
And there is nothing wrong with writing entire applications in nothing but assembler.

Posted: 30 May 2007, 09:47
by Hex_Rated
And there is nothing wrong with writing entire applications in nothing but assembler.
8O OK, could you write an application in assembler that connects to an Oracle 10g database, allows you to browse and edit a table displaying the information on text boxes on a form in under 20 minutes? What if the client wanted to change it to a MySQL database? How long would it take?

Don't get me wrong, assembler is extremely important since it is as close to a CPU's native language as you can get without replacing the opcodes with numbers but in this day and age it is not very useful outside engineering. Even when working with drivers for embedded micro controllers, I hardly ever come across it any more. Shaders for graphics cards were written in assembler, even they are using Cg or HLSL these days AFAIK.

Posted: 30 May 2007, 10:11
by PHR33K
I dont use databases, and if I were to then, yes, something like C# would be better.

I write all drivers in about 60% inline assembly the rest in C.
I've written entire rootkits in assembly as well.

Writing gui's in assembly, only really takes a bit longer than doing it in C/C++.

Every programmer should know at least basic assembly (for disassembly purposes)

Posted: 30 May 2007, 10:26
by Hex_Rated
What type of apps / drivers do you write?

Posted: 30 May 2007, 10:55
by PHR33K
Currently, I'm busy writing a punkbuster bypass.
I write allot of security related applications as well.
I recently wrote a fairly decent packet editor that I was rather proud of.

Posted: 30 May 2007, 11:12
by Hex_Rated
Punkbuster bypass? You planning on cheating in battlefield or something? :)

If you're reverse engineering and cracking, assembler is definitely the way to go. For most commercial applications, you wouldn't encounter it unless (as you said) you need to view the disassembly for debugging purposes, which is rare.

Posted: 30 May 2007, 11:41
by PHR33K
Yep :D

I've written a few anti-cheat bypasses... Its fun 8)