1

I am having a pre defined list of machines and a constant duration in minutes.
Each machine is started mechanically when it does my program receives an input from the machine and I am storing the machine id, start time , added value of duration with start time as end time, duration and status of the machine in sql server database.
in the win forms screen using vb .net and listview i am displaying the data from the table and refreshing it through a timer. The timer interval is one second. the query is also calculating the elapsed time and displaying in the listview. and from UI when duration and elapsed minutes reaches equal program send a signal to update the database .now is it wise to use timer to this activity or threading.task which is more efficient. any help is greatly appriciated. for ref the code with query is

Sub loadfromtable()
    Try
        ListView1.Items.Clear()
        Dim qry As String = "select *,DATEDIFF(second,starttime,cast(GETDATE()AS time))/60 as etime from tblconsole  where status='A' AND DATEDIFF(second,starttime,cast(GETDATE()AS time))>0"
        'old qry --"select *,'' as [etime] from tblconsole where status='A'"
    Using db As New sqlDataclass
            Dim dt As DataTable = db.bindData(qry)
        If Not dt Is Nothing AndAlso dt.Rows.Count > 0 Then
            For Each r As DataRow In dt.Rows
                Dim itm As New ListViewItem(r(0).ToString)
                    itm.SubItems.Add(r(1).ToString())
                    itm.SubItems.Add(r(2).ToString())
                    itm.SubItems.Add(r(4))
                    itm.SubItems.Add(r(3))
                    itm.SubItems.Add(r("etime"))
                ListView1.Items.Insert(0, itm)

            Next
        End If
        End Using
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try
End Sub
i3arnon
  • 113,022
  • 33
  • 324
  • 344
sansknwoledge
  • 4,209
  • 9
  • 38
  • 61

2 Answers2

0

You showed the code for getting the data not updating the data based on the timer. If you are not showing anything to the user on the timer event elapsed event you won't run into thread problems.

There is nothing technically wrong with using a timer in your case. Have a look at at discussion here on timer vs TPL: Proper way to implement a never ending task. (Timers vs Task)

Community
  • 1
  • 1
Marcin Waligora
  • 548
  • 4
  • 11
  • I am just planning to add an update routine inside the timer my thought is basically just check equality for the column 3 the duration and the elapsed duration at column 5 and if column 5 equals or exceeds then send a update command to the table to change the status . will it affect the process or performance – sansknwoledge Jan 22 '14 at 07:16
0

Don't use a timer.

A timer simply fires an event every second, it doesn't care how much time your work (updating the DB) takes. Let's assume it takes 0.5 seconds, that means that you'll be updating for half a second and waiting for half a second. If it takes more than a second then you'll be updating in parallel from 2 threads, which i guess you don't want to do.

Use a thread that waits for a second and then does the work. You can do that with a Task and if you use .Net 4.5 you can use async-await so the task waits asynchronously without using up a thread.

i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • ok, now i think, shall i have a timer to constantly update the listview and a task to update the database , the task is necessarily triggered from the timer.tick event , will this hold good? thanks – sansknwoledge Jan 22 '14 at 10:50
  • You can, because updating a listview will probably take much less than a second, but i suggest always using some kind of periodic task that sleeps (or `await`s) instead of a timer. – i3arnon Jan 22 '14 at 10:52
  • sorry being noob ,is there any specific reason to not to use timers , could you throw some pointers? – sansknwoledge Jan 22 '14 at 14:02
  • @sansknwoledge what i said in my answer. the timer fires every second no matter what. The task waits for a second between the end of the previous run to the start of the current one. If the work takes more than a second the work will happen concurrently – i3arnon Jan 22 '14 at 14:05
  • @l3arnon, i met with cross threading violation if i try to iterate through the listview while using task , i tried to invoke it but it is not throwing any error and hangs. the code is here http://pastebin.com/Sgaf8DnL – sansknwoledge Jan 23 '14 at 07:11
  • @sansknwoledge If you have a new problem you should ask a new question so everybody could see it. For example, I write C#, not VB. but I'll try and take a look. – i3arnon Jan 23 '14 at 07:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45876/discussion-between-sansknwoledge-and-i3arnon) – sansknwoledge Jan 23 '14 at 07:18