在使用threading.timer和webcontrol时,Ifinvoke需要并形成冻结的问题 [英] Problem With Ifinvokerequired and form freezing when using threading.timer and webcontrol

查看:81
本文介绍了在使用threading.timer和webcontrol时,Ifinvoke需要并形成冻结的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我一直试图弄清楚为什么我的表单会在我的代码开始工作后一直冻结。我试图使用线程计时器与代理更新主UI,但每秒检查一个网页时保持它没有滞后。请帮忙!



代表子



Hello I have been trying to figure out why my form keeps freezing once my code starts to do work. I am trying to use threading timer with delegates to update the main UI but by keeping it free of lags when it checks a webpage every second. Please help!

Delegate sub

Public Shared Sub SetControlText(ByVal ctrl As Control, ByVal value As String)
        If ctrl.InvokeRequired Then
            ctrl.Invoke(New delSetControlText(AddressOf SetControlText), ctrl, value)
        Else
            ctrl.Text = value
        End If
    End Sub





In我的全局变量



In my global variables

Public KeepAliveDelegate As Threading.TimerCallback
   Public KeepAliveTimer As System.Threading.Timer  
Public Shared webbrowserControl As New System.Windows.Forms.WebBrowser
    Public Delegate Sub delSetControlText(ByVal ctrl As Control, ByVal value As String)





这是我的表单加载事件



This is in my form load event

KeepAliveDelegate = AddressOf KeepAliveTimerHandler
        KeepAliveTimer = New System.Threading.Timer(KeepAliveDelegate, Nothing, 1000, 1000)
        KeepAliveTimer.Change(1000, 1000)







Private Sub KeepAliveTimerHandler(ByVal state As Object)
     Me.Invoke(Sub() GetData())
  End Sub

 Public Sub getData()
            Dim allelem As HtmlElementCollection = webbrowserControl.Document.All
            For Each webelem As HtmlElement In allelem
                If webelem.GetAttribute("id") = "log" Then
                'Dim logtext As String = webelem.InnerText
                    Dim loghtml As String = webelem.InnerHtml
                    For Each DIS As Match In ServerLost.Matches(loghtml)
                        If DIS.Success Then
                            webbrowserControl.Navigate(Address)
                        End If
                    Next
                    For Each m As Match In md5hash.Matches(loghtml)
                        If m.Success Then
                            Dim sk As String = NetCheck(Address, "" & m.Value)
                       SetControlText( RichTextBox1,sk)
                        End If
                    Next
                End If
            Next
    End Sub





我已经看到了这个问题的一些其他解释,但我仍然坚持为什么它发生在我的情况< br $> b $ b



提前谢谢!



I have seen a few other explanations to this problem but im still stuck as to why its happening in my case


thank you in advance!

推荐答案

这篇关于在使用threading.timer和webcontrol时,Ifinvoke需要并形成冻结的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆