计时器无法在vb.net中启动

时间:2013-08-31 13:17:59

标签: vb.net timer refresh

使用excel作为数据源的网络程序。然后,我使用此数据源填充datagridview,并通过datagridview对数据集进行更改。我正在尝试通过一个按钮来刷新此数据集,该按钮将在更改后更新值。我唯一的问题是我正在尝试在我的刷新方法中设置一个计时器,但它从不初始化/启动。我无法弄清楚为什么,从我在网上找到的,在vb.net中启动计时器的方法是将timer变量设置为enabled = true。我已经进入调试器,发现计时器永远不会启动。以下是我的代码,如果有人能够弄清楚为什么这个计时器没有启动我会非常感谢你的帮助!

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

1 个答案:

答案 0 :(得分:1)

您正在此处创建计时器 Dim mytimer As New System.Timers.Timer 但是你只为你在Refresh例程中创建的处理程序分配一个处理程序。

预期

Dim mytimer As New System.Timers.Timer(20000)
AddHandler mytimer.Elapsed, AddressOf OnTimedEvent

然后

Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)

   Console.WriteLine("The Elapsed event was raised at {0}, e.SignalTime)
   Refresh()
End Sub

等等

Sub refresh()

   myTimer.Enabled = False
   // refresh the doings
   myTimer.Enabled = True
End sub

原谅缺乏VBness,我是C#男孩。