Vb.net 如何解决BackgroundWorker更新表单导致的速度减慢?

Vb.net 如何解决BackgroundWorker更新表单导致的速度减慢?,vb.net,multithreading,forms,backgroundworker,Vb.net,Multithreading,Forms,Backgroundworker,我正在使用VB.Net。我希望我的所有表单都能检查是否存在与服务器的连接,以及当没有连接时,是否会显示一个可视的PictureBox。当存在连接时,代码可以正常工作,但如果不是每五秒钟就冻结一次表单。首先,我认为这是因为我调用Threading.Thread.Sleep(5000),但当有连接时,这一行也会执行,所以我不明白为什么只有在没有连接时才会发生这种情况。可能是因为更改可见属性而发生的?我已经试过了,这个属性只有在必要时才会改变 Private Sub Form1_Load() Han

我正在使用VB.Net。我希望我的所有表单都能检查是否存在与服务器的连接,以及当没有连接时,是否会显示一个可视的PictureBox。当存在连接时,代码可以正常工作,但如果不是每五秒钟就冻结一次表单。首先,我认为这是因为我调用Threading.Thread.Sleep(5000),但当有连接时,这一行也会执行,所以我不明白为什么只有在没有连接时才会发生这种情况。可能是因为更改可见属性而发生的?我已经试过了,这个属性只有在必要时才会改变


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
Private Sub Form1\u Load(发送方作为对象,e作为事件参数)处理MyBase.Load
Dim t As Task=Task.Run(Sub()ConnectedAnimation())
端接头
专用子连接动画()
做
线程。线程。睡眠(5000)
如果未连接(),则
Me.BeginInvoke(Sub()offlinePictureBox.Visible=False)
其他的
Me.BeginInvoke(Sub()offlinePictureBox.Visible=True)
如果结束
环
端接头
私有函数isConnected()为布尔值
将rv标注为布尔值=False
' >
'rv=DAOUtils.ExistsServerConnection()
返回rv
端函数

您的代码过于分散,但看起来很好。您需要提供我们可以用来复制您的问题以解决问题的解决方案。你能提供一个吗?我认为
DAOUtils.ExistsServerConnection()
是你需要在工作线程中调用的。您的代码现在在主线程中调用它
DAOUtils.ExistsServerConnection()
需要从BGW线程调用,而不是从UI线程调用。2.从
Me中删除
Me
。指示IfexistsConnection()
3。使用BGW的方法:在UI线程中引发相关事件;不要从BGW线程4中
Invoke()
BeginInvoke()
。当您
BeginInvoke()
(通常不在此处)时,您不需要检查
InvokeRequired
,并且您的目标封送拆收器是一个表单,而不是子控件(此处为PictureBox)。因此:1。预先设定2。订阅活动。3.引发事件调用
ReportProgress()
,必要时在
UserState
参数中传递一个值。4.在
ProgressChanged
处理程序中,仅更改
offlinePictureBox
可见性状态。没有别的了。