Multithreading System.timers.timer-无法随时间推移更新标签

Multithreading System.timers.timer-无法随时间推移更新标签,multithreading,timer,thread-safety,Multithreading,Timer,Thread Safety,我想在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 'Counte

我想在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
试过上面的代码,但不起作用, 我想在用户单击“处理”按钮后立即更新标签控件上的运行时间
直到所有的功能在点击按钮时被执行


请提供帮助。

由于
System.Timers.Timer
在单独的线程上运行,因此无法直接更新GUI。您需要在控件上使用
BeginInvoke
将更新转发到GUI线程。不幸的是,我对vb.net不是很熟悉,但您可以在这里找到一个如何操作的示例:

不允许您从工作线程更新UI。运行已用事件处理程序的类型。改为使用System.Windows.Forms.Timer。谢谢,Hans尝试使用System.Windows.Forms.Timer,但它显示了所有函数执行后经过的时间,因为我希望用户在单击“处理”按钮时可以看到经过的时间。这就是错误操作时发生的情况。使用BackgroundWorker执行长时间运行的操作。使用其ProgressChanged事件显示进度。让UI线程只负责更新UI。