1

我想在 Windows 窗体上的标签控件上显示经过的时间。为此,我正在使用 System.Timers.timer 但无法使用按钮单击事件的经过时间来更新标签。

例如

Private Shared mtimer As System.Timers.Timer

Private Sub btnprocess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprocess.Click

'Counter to Calculate time as Process Starts
 mDate = Date.Now
 ''Create a timer with  second interval
 mtimer = New System.Timers.Timer()
 ''Hook the Elapsed event
 **AddHandler mtimer.Elapsed, AddressOf Processtick**
 mtimer.Start()
'set the interval 1 second 
 mtimer.Interval = 1000
 mtimer.Enabled = True

 ''Some functions 

   end sub 

Private Sub Processtick(ByVal source As Object, ByVal e As ElapsedEventArgs)

Dim ts As TimeSpan = DateTime.Now.Subtract(mDate)
lblelapsed.Text = ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds

End sub

尝试了上面的代码,但它不起作用,我想在用户单击“处理”按钮后立即更新标签控件上的经过时间,
直到所有功能在按钮单击时执行。

请帮忙。

4

1 回答 1

0

由于System.Timers.Timer在单独的线程上运行,它不能直接更新 GUI。您需要使用BeginInvoke控件将更新转发到 GUI 线程。不幸的是,我对 vb.net 不是很熟悉,但您可以在此处找到如何操作的示例:http: //msdn.microsoft.com/en-us/library/0b1bf3y3.aspx

于 2012-10-08T13:10:37.163 回答