计时器未在vb.net中初始化 [英] Timer not initializing in vb.net
问题描述
您好我有一个使用excel作为数据源的vb.net程序。然后,我使用此数据源填充datagridview,并通过datagridview对数据集进行更改。我正在尝试通过一个按钮来刷新此数据集,该按钮将在更改后更新值。我唯一的问题是我正在尝试在我的刷新方法中设置一个计时器,但它从不初始化/启动。我无法弄清楚为什么,从我在网上找到的,在vb.net中启动计时器的方法是将timer变量设置为enabled = true。我已经进入调试器,发现计时器永远不会启动。以下是我的代码,如果有人能够弄清楚为什么这个计时器没有启动我会非常感谢你的帮助!
'声明我的计时器变量
Dim mytimer As 新 System.Timers.Timer
Sub refresh()
write2Size()
mytimer.timer = < span class =code-keyword>新 System.Timers.Timer( 20000 )
' 启动计时器
mytimer.Enabled = True
光标。 Current = Cursors.WaitCursor
AddHandler mytimer.Elapsed, AddressOf OnTimedEvent
objworkbook.Save()
objExcel.ActiveWorkbook.Save()
myDS.Clear( )
retrieveUpdate()
' 将光标设置回正常值
Cursor.Current =游标。默认
结束 Sub
私人 共享 Sub OnTimedEvent(source As Object ,e As ElapsedEventArgs)
Console.WriteLine(& quot; Elapsed event 在{ 0 },e.SignalTime)
结束 Sub
这对我有用。计时器每20秒触发一次Elapsed
事件。
Dim mytimer As 新 System.Timers.Timer
Sub MyRefresh()
write2Size()
mytimer = 新 System.Timers.Timer( 20000 )
' 启动计时器
AddHandler mytimer.Elapsed, AddressOf OnTimedEvent
mytimer.Enabled = True
Cursor.Current = Cursors.WaitCursor
objworkbook.Save()
objExcel.ActiveWorkbook.Save()
myDS.Clear()
retrieveUpdate()
' 将光标设置回来这里正常
Cursor.Current =游标。默认
结束 Sub
私人 共享 Sub OnTimedEvent(source As Object ,e As ElapsedEventArgs)
Console.WriteLine( Elapsed事件的引发时间为{0},e.SignalTime)
结束 子跨度>
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
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
This works for me. The timer fires theElapsed
event every twenty seconds.
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
这篇关于计时器未在vb.net中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!