使用计时器更新表单-VB.Net
我想在表单上显示我的应用程序的各个方面的状态 我尝试在表单中添加计时器。计时器每10秒运行一次,检查数据库的特定状态,然后根据状态更改表单上的元素 这对于更改菜单项的颜色很有效,但当我尝试在表单上显示图像(in)时,会出现交叉线程错误 我应该使用计时器、后台工作人员还是其他什么 有没有人有一个基本的代码可以优雅地完成这项工作 Private WithEvents TMR作为新的System.Timers.Timer(10000) 尝试:使用计时器更新表单-VB.Net,vb.net,multithreading,forms,timer,Vb.net,Multithreading,Forms,Timer,我想在表单上显示我的应用程序的各个方面的状态 我尝试在表单中添加计时器。计时器每10秒运行一次,检查数据库的特定状态,然后根据状态更改表单上的元素 这对于更改菜单项的颜色很有效,但当我尝试在表单上显示图像(in)时,会出现交叉线程错误 我应该使用计时器、后台工作人员还是其他什么 有没有人有一个基本的代码可以优雅地完成这项工作 Private WithEvents TMR作为新的System.Timers.Timer(10000) 尝试: 你能提供一些代码吗?添加的代码很基本,但应该能给你一些想法
你能提供一些代码吗?添加的代码很基本,但应该能给你一些想法。
Private Sub frmMaster_Load(sender As Object, e As EventArgs) Handles Me.Load
tmrMain.Enabled = True
tmrMain.Start()
End Sub
Private Sub tmrMain_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmrMain.Elapsed
Dim TRunning As Boolean = True
TRunning = BTools.CheckImportRunningStatus '' This gets the on/off status from the DB
If TRunning Then
miFileMYOBImport.ForeColor = Color.Red '' Change menu color
pbMYOBDownload.Visible = True '' Make image visible
Else
miFileMYOBImport.ForeColor = Color.DarkGreen
pbMYOBDownload.Visible = False
End If
End Sub
Private Sub MakeImageVisible(ByVal control As Control, ByVal visible As Boolean)
If control.InvokeRequired Then
control.BeginInvoke(New Action(Of Control, Boolean)(AddressOf MakeImageVisible), control, visible)
Else
control.Visible = visible
End If
End Sub
Private Sub tmrMain_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmrMain.Elapsed
Dim TRunning As Boolean = True
TRunning = BTools.CheckImportRunningStatus '' This gets the on/off status from the DB
If TRunning Then
miFileMYOBImport.ForeColor = Color.Red '' Change menu color
Else
miFileMYOBImport.ForeColor = Color.DarkGreen
End If
MakeImageVisible(pbMYOBDownload, TRunning) ' Make image visible or invisible
End Sub