-
Mar 31st, 2014, 05:52 AM
#1
Thread Starter
Addicted Member
Stopwatch in my application
For test purpose, I want to measure time that my application (it has one form) takes to display data in listbox.
I want to see , if I click btnDisplay, the stopwatch starting, and when data is displayed, the stopwatch stopping.
Thank you.
This is the code so far:
Code:
Option Strict On
Public Class Form1
Dim StopWatch As New Diagnostics.Stopwatch
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim elapsed As TimeSpan = Me.StopWatch.Elapsed
Label1.Text = String.Format("{0:00}:{1:00}:{2:00}:{3:00}", _
Math.Floor(elapsed.TotalHours), _
elapsed.Minutes, elapsed.Seconds, _
elapsed.Milliseconds)
-
Mar 31st, 2014, 06:28 AM
#2
Fanatic Member
Re: Stopwatch in my application
Just use the .Start and .Stop methods for stopwatch.
Something like:
Code:
Imports System.Diagnostics
Public Class Form1
Dim stopWatch As New Stopwatch()
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
stopWatch.Start()
'Do your stuff here
stopWatch.Stop()
Dim ts As TimeSpan = stopWatch.Elapsed
Dim elapsedTime As String = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
MsgBox(elapsedTime)
stopWatch.Reset()
End Sub
End Class
Last edited by Flashbond; Mar 31st, 2014 at 06:32 AM.
God, are you punishing me because my hair is better than yours? -Jack Donaghy
-
Mar 31st, 2014, 06:34 AM
#3
Thread Starter
Addicted Member
Re: Stopwatch in my application
Thank you very much, flashbond.
I'll try it.
-
Mar 31st, 2014, 06:47 AM
#4
Thread Starter
Addicted Member
Re: Stopwatch in my application
Your code works.
I want to see time running in label, not just totalElapsedTime.
Could this be possible? Thanks
-
Mar 31st, 2014, 06:57 AM
#5
Re: Stopwatch in my application
Move the stopwatch to the namespace and in your timer show the elapsed time.
-
Mar 31st, 2014, 06:59 AM
#6
Thread Starter
Addicted Member
Re: Stopwatch in my application
Thank you very much.
I'll give it a try.
-
Mar 31st, 2014, 07:30 AM
#7
Fanatic Member
Re: Stopwatch in my application
You may have to run another thread to display eplased time properly in a label. It depends on the frequncy of sampling.
God, are you punishing me because my hair is better than yours? -Jack Donaghy
-
Mar 31st, 2014, 07:38 AM
#8
Thread Starter
Addicted Member
Re: Stopwatch in my application
It's not necessary. I'll stick to my application, with that simple stopwatch code.
-
Mar 31st, 2014, 08:36 AM
#9
Fanatic Member
Re: Stopwatch in my application
This is a common GUI problem.
Here, watch this video: http://www.youtube.com/watch?v=CitR-Fok3HA
This is a brief example what I am trying to say.
And never use "CheckForIllegalCrossThreadCalls = False" like he did in the video, ever!
God, are you punishing me because my hair is better than yours? -Jack Donaghy
-
Mar 31st, 2014, 08:42 AM
#10
Thread Starter
Addicted Member
Re: Stopwatch in my application
Sorry, I thought you were asking me to post a new thread.
And thank you again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|