BackgroundWorker更新Form导致速度变慢如何解决?
How to solve the slowdown caused by BackgroundWorker to update the Form?
我正在使用 VB.Net。我希望我的所有表单都可以检查是否存在与服务器的连接,以及何时没有表明它正在执行可见的 PictureBox。有连接时代码工作正常,但大约每五秒冻结一次表单。首先,我想这是因为我调用了 Threading.Thread.Sleep(5000),但是当有连接时,这行也会被执行,所以我不明白为什么只有在没有连接时才会发生这种情况。可能是因为更改 Visible 属性?我已尝试确保此 属性 仅在必要时更改。
Private Sub Form1_Load() Handles Me.Load
backgroundWorker.RunWorkerAsync()
End Sub
Private Sub backgroundWorker_DoWork(sender As Object, e As DoWorkEventArgs) Handles backgroundWorker.DoWork
While True
Me.IndicateIfExistsConnection()
Threading.Thread.Sleep(5000)
End While
End Sub
Private Sub IndicateIfExistsConnection()
If Me.offlinePictureBox.InvokeRequired Then
Me.offlinePictureBox.BeginInvoke(Sub() IndicateIfExistsConnectionMustBeOnUIThread())
Else
IndicateIfExistsConnectionMustBeOnUIThread()
End If
End Sub
Private Sub IndicateIfExistsConnectionMustBeOnUIThread()
If Not DAOUtils.ExistsServerConnection() Then MakeVisibleOfflineWarning() Else MakeInvisibleOfflineWarning()
End Sub
Private Sub MakeVisibleOfflineWarning()
If Not offlinePictureBox.Visible Then offlinePictureBox.Visible = True
End Sub
Private Sub MakeInvisibleOfflineWarning()
If offlinePictureBox.Visible Then offlinePictureBox.Visible = False
End Sub
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
backgroundWorker.CancelAsync()
End Sub
编辑:
我试过@Jimi和@dbasnett的方案。两者都很好。最后我使用了@Jimi 的解决方案,虽然我不知道我是否理解它。现在的代码是这样的:
Private Sub Form1_Load() Handles Me.Load
backgroundWorker.RunWorkerAsync()
End Sub
Private Sub backgroundWorker_DoWork(sender As Object, e As DoWorkEventArgs) Handles backgroundWorker.DoWork
While True
backgroundWorker.ReportProgress(0, DAOUtils.ExistsServerConnection())
Threading.Thread.Sleep(1000)
End While
End Sub
Private Sub backgroundWorker_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles backgroundWorker.ProgressChanged
Dim existsServerConnection As Boolean = e.UserState
If existsServerConnection Then offlinePictureBox.Visible = False Else offlinePictureBox.Visible = True
End Sub
可能是我没看懂问题。但根据我的理解,这可能会成功。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim t As Task = Task.Run(Sub() ConnectedAnimation())
End Sub
Private Sub ConnectedAnimation()
Do
Threading.Thread.Sleep(5000)
If isConnected() Then
Me.BeginInvoke(Sub() offlinePictureBox.Visible = False)
Else
Me.BeginInvoke(Sub() offlinePictureBox.Visible = True)
End If
Loop
End Sub
Private Function isConnected() As Boolean
Dim rv As Boolean = False
' <<<<< code to check connection here >>>>>
' rv = DAOUtils.ExistsServerConnection()
Return rv
End Function
我正在使用 VB.Net。我希望我的所有表单都可以检查是否存在与服务器的连接,以及何时没有表明它正在执行可见的 PictureBox。有连接时代码工作正常,但大约每五秒冻结一次表单。首先,我想这是因为我调用了 Threading.Thread.Sleep(5000),但是当有连接时,这行也会被执行,所以我不明白为什么只有在没有连接时才会发生这种情况。可能是因为更改 Visible 属性?我已尝试确保此 属性 仅在必要时更改。
Private Sub Form1_Load() Handles Me.Load
backgroundWorker.RunWorkerAsync()
End Sub
Private Sub backgroundWorker_DoWork(sender As Object, e As DoWorkEventArgs) Handles backgroundWorker.DoWork
While True
Me.IndicateIfExistsConnection()
Threading.Thread.Sleep(5000)
End While
End Sub
Private Sub IndicateIfExistsConnection()
If Me.offlinePictureBox.InvokeRequired Then
Me.offlinePictureBox.BeginInvoke(Sub() IndicateIfExistsConnectionMustBeOnUIThread())
Else
IndicateIfExistsConnectionMustBeOnUIThread()
End If
End Sub
Private Sub IndicateIfExistsConnectionMustBeOnUIThread()
If Not DAOUtils.ExistsServerConnection() Then MakeVisibleOfflineWarning() Else MakeInvisibleOfflineWarning()
End Sub
Private Sub MakeVisibleOfflineWarning()
If Not offlinePictureBox.Visible Then offlinePictureBox.Visible = True
End Sub
Private Sub MakeInvisibleOfflineWarning()
If offlinePictureBox.Visible Then offlinePictureBox.Visible = False
End Sub
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
backgroundWorker.CancelAsync()
End Sub
编辑:
我试过@Jimi和@dbasnett的方案。两者都很好。最后我使用了@Jimi 的解决方案,虽然我不知道我是否理解它。现在的代码是这样的:
Private Sub Form1_Load() Handles Me.Load
backgroundWorker.RunWorkerAsync()
End Sub
Private Sub backgroundWorker_DoWork(sender As Object, e As DoWorkEventArgs) Handles backgroundWorker.DoWork
While True
backgroundWorker.ReportProgress(0, DAOUtils.ExistsServerConnection())
Threading.Thread.Sleep(1000)
End While
End Sub
Private Sub backgroundWorker_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles backgroundWorker.ProgressChanged
Dim existsServerConnection As Boolean = e.UserState
If existsServerConnection Then offlinePictureBox.Visible = False Else offlinePictureBox.Visible = True
End Sub
可能是我没看懂问题。但根据我的理解,这可能会成功。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim t As Task = Task.Run(Sub() ConnectedAnimation())
End Sub
Private Sub ConnectedAnimation()
Do
Threading.Thread.Sleep(5000)
If isConnected() Then
Me.BeginInvoke(Sub() offlinePictureBox.Visible = False)
Else
Me.BeginInvoke(Sub() offlinePictureBox.Visible = True)
End If
Loop
End Sub
Private Function isConnected() As Boolean
Dim rv As Boolean = False
' <<<<< code to check connection here >>>>>
' rv = DAOUtils.ExistsServerConnection()
Return rv
End Function