Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello I have a vb.net program that uses excel as a datasource. I then fill a datagridview with this datasource and make changes to the dataset via the datagridview. I'm trying to find a way to refresh this dataset via a button that will update the values after a change. My only problem is that I'm trying to set up a timer in my refresh method but it never initializes/starts. I can't figure out why, from what I've found online the way to start a timer in vb.net is to set the timer variable to enabled = true. I've stepped into my debugger and found that the timer never starts. Here is my code below, if there is anyone who can figure out why this timer isn't starting I would greatly appreciate your help!


'declare my timer variable
VB
Dim mytimer As New System.Timers.Timer
Sub refresh()
      
       write2Size()
       


       mytimer.timer = New System.Timers.Timer(20000)
      'Starting Timer
       mytimer.Enabled = True
       Cursor.Current = Cursors.WaitCursor


     
       AddHandler mytimer.Elapsed, AddressOf OnTimedEvent

       
 
       objworkbook.Save()
       objExcel.ActiveWorkbook.Save()

       myDS.Clear()
       retrieveUpdate()

'Setting the cursor back to normal here
       Cursor.Current = Cursors.Default
   End Sub

   Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
    
       Console.WriteLine("The Elapsed event was raised at {0}, e.SignalTime)
   End Sub
Posted
Updated 31-Aug-13 3:43am
v2
Comments
[no name] 31-Aug-13 9:23am    
If you get actual compilable code working, you would find that your timer is starting. You are starting your timer on the line "mytimer.Enabled = True" and then for some inexplicable reason, 4 lines later, you are disabling the timer.
CAS1224 31-Aug-13 9:43am    
Sorry about that
[no name] 31-Aug-13 9:57am    
Okay so now that you have updated your question, how is it that you know (or think) that your timer never starts?
CAS1224 31-Aug-13 10:04am    
The cursor never changes
[no name] 31-Aug-13 11:48am    
Ah... so you in fact do not know if your timer is actually starting or not. First thing you need to do is to get your code to compile. This code presented here will not compile much less run. Then run your code under the control of the debugger and find out exactly what is going on with your code. I would also be curious as to how you are displaying a datagridview in a console application. Or, why you need to display a console in a Windows application. But that is something else entirely.

1 solution

This works for me. The timer fires the Elapsed event every twenty seconds.
VB
Dim mytimer As New System.Timers.Timer
Sub MyRefresh()
    write2Size()
    mytimer = New System.Timers.Timer(20000)
    'Starting Timer
    AddHandler mytimer.Elapsed, AddressOf OnTimedEvent
    mytimer.Enabled = True
    
    Cursor.Current = Cursors.WaitCursor

    objworkbook.Save()
    objExcel.ActiveWorkbook.Save()

    myDS.Clear()
    retrieveUpdate()

    'Setting the cursor back to normal here
    Cursor.Current = Cursors.Default
End Sub

Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
    Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime)
End Sub
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900