Question Create a timer and assign it a function for each row of data gotten from a SQL query

Francismori7

New member
Joined
Dec 31, 2009
Messages
4
Programming Experience
1-3
Basically, I want to create a timer for each row of data that will get returned by a SQL query:

SELECT * FROM Projects

I would want the timers to be called "Compteur" & ROWID. This really is necessary.

I need them to have different Ticks and so that some can be enabled all at once. Basically it is for a project tracker to count the time you are working on the current project. However you could indeed work on many project at the same time.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,743
Location
Sydney, Australia
Programming Experience
10+
Timers don't have names. You might declare a variable and assign a Timer to that variable, but that's not a name.
VB.NET:
Dim t1 As New Timer
Dim t2 As Timer = t1
Dim t3 As Timer = t2
There you have one Timer object and three Timer variables: t1, t2 and t3? Which one of those is the name of the Timer? The answer is that none of them are. Timers don't have names.

If you don't know how many rows there are going to be ahead of time then you can't even declare a variable for each one. All you can do is declare a single variable and have it refer to an object that can contain all the Timers you need create. That would most likely be a collection of some sort, e.g. a List or Dictionary. Here's an example that uses a List:
VB.NET:
Public Class Form1

    Private timers As New List(Of Timer)
    Private table As New DataTable

    Private Sub Form1_Load(ByVal sender As Object, _
                           ByVal e As EventArgs) Handles MyBase.Load
        'Populate table here.

        Dim rng As New Random

        For Each row In Me.table.Rows
            Dim t As New Timer

            t.Interval = rng.Next(2000, 5000)
            AddHandler t.Tick, AddressOf Timer_Tick
            t.Start()
        Next
    End Sub

    Private Sub Timer_Tick(ByVal sender As Object, _
                           ByVal e As EventArgs)
        'Get a reference to the Timer that just Ticked.
        Dim t = DirectCast(sender, Timer)

        t.Stop()

        'Get the row at the same index as the Timer.
        Dim row = Me.table.Rows(Me.timers.IndexOf(t))

        Console.WriteLine(CStr(row("Name")))
    End Sub

End Class
That said, I don't think that it's actually a Timer that you want. Timers, despite the name, are not for measuring time. They are like an alarm clock. They are for taking some action at regular intervals. If you want to actually measure an arbitrary time period then what you want is a Stopwatch. Stopwatches can measure time from when you Start to when you Stop. Just like a physical stopwatch, they support pausing and resetting.
 

Francismori7

New member
Joined
Dec 31, 2009
Messages
4
Programming Experience
1-3
Then if I want a stopwatch, how would I do that? Change them timers to stopwatches by changing "Timer" to stopwatch?

Also, is it possible to use a listview to display every row of the table, then when a row is selected, the row will automatically get the data from the table and then a button will be displayed and that one could be used to stop/start the timer. Would it?

Thanks
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,743
Location
Sydney, Australia
Programming Experience
10+
The Stopwatch class is very different to the Timer class. You should start by reading the documentation for the Stopwatch class in the MSDN Library. If you still have questions, then you can post them here.
 
Top Bottom