VB.Net中从numericupdown开始的随机延迟(以秒为单位)

时间:2014-12-21 22:19:32

标签: vb.net random delay

我有一个桌面winforms应用代码:

    Sub Delay(ByVal dblSecs As Double)
        Const OneSec As Double = 1.0# / (1440.0# * 60.0#)
        Dim dblWaitTil As Date
        Now.AddSeconds(OneSec)
        dblWaitTil = Now.AddSeconds(OneSec).AddSeconds(dblSecs)
        Do Until Now > dblWaitTil
            Application.DoEvents()
        Loop
    End Sub

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Webbrowser1.Navigate(TextBox1.Text)
Delay(Val(DelayText.Text))
end sub

我需要的是设置最大值。延迟,从文本框输入秒数相同。 我需要这是随机延迟号码,所以我在这里堆叠,谢谢你纠正我的代码。 另外如果它可以在NumericUpDown中进行,因为我发现一些主题说文本框文本属性与NumericUpDown不同,但我更喜欢它。

1 个答案:

答案 0 :(得分:0)

您可以使用inbetween类来完成大部分工作

此类将使用WebBrowser,附加到它的某些事件,并定期刷新(取决于MinimumWait / MaximumWait)

由于它正在使用线程,它还会检查是否需要调用usercontrol来刷新它,如果是,则调用自定义刷新委托

Public Class Refresher
    Protected Delegate Sub RefreshNavigationDelegate(browser As WebBrowser)

    Protected Sub RefreshNavigation(browser As WebBrowser)
        If browser.InvokeRequired Then
            browser.Invoke(New RefreshNavigationDelegate(AddressOf RefreshNavigation), browser)
            Return
        End If
        browser.Refresh(WebBrowserRefreshOption.Completely)
    End Sub

    Private _isBusy As Boolean = False
    Public Property IsBusy As Boolean
        Get
            Return _isBusy
        End Get
        Protected Set(value As Boolean)
            If _isBusy = value Then
                Return
            End If
            _isBusy = value
        End Set
    End Property

    Public Property MinimumWait As Integer = 2000

    Public Property MaximumWait As Integer = 10000

    Private refreshThread As Thread = Nothing

    Private _browser As WebBrowser
    Public Property Browser As WebBrowser
        Get
            Return _browser
        End Get
        Set(value As WebBrowser)
            If Object.Equals(_browser, value) Then
                Return
            End If
            StopRefresh()
            If _browser IsNot Nothing Then
                RemoveHandler Browser.DocumentCompleted, AddressOf DocumentComplete
                RemoveHandler Browser.Navigating, AddressOf Navigating
            End If
            _browser = value
            If _browser IsNot Nothing Then
                AddHandler Browser.DocumentCompleted, AddressOf DocumentComplete
                AddHandler Browser.Navigating, AddressOf Navigating
                AddHandler Browser.ProgressChanged, AddressOf ProgressChanged
            End If
            StartRefresh()
        End Set
    End Property

    Protected Sub ProgressChanged(sender As Object, e As WebBrowserProgressChangedEventArgs)
        IsBusy = e.CurrentProgress > 0 AndAlso e.CurrentProgress < e.MaximumProgress
    End Sub

    Protected Sub DocumentComplete(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
        IsBusy = False
    End Sub

    Protected Sub Navigating(sender As Object, e As WebBrowserNavigatingEventArgs)
        IsBusy = True
    End Sub

    Public Sub StartRefresh()
        If refreshThread IsNot Nothing Then
            Return
        End If
        refreshThread = New Thread(AddressOf DoRandomRefreshes)
        refreshThread.Start()
    End Sub

    Public Sub StopRefresh()
        If refreshThread Is Nothing Then
            Return
        End If
        refreshThread.Abort()
        refreshThread = Nothing
    End Sub

    Protected Overridable Sub DoRandomRefreshes()
        Dim randomGenerator As New Random()

        While Not refreshThread.ThreadState = ThreadState.AbortRequested
            Dim newTimeout As Integer = MinimumWait + randomGenerator.Next(MaximumWait - MinimumWait)
            Thread.Sleep(newTimeout)
            If Not IsBusy Then
                RefreshNavigation(Browser)
            End If
        End While
    End Sub

    Public Sub New()

    End Sub
End Class

然后您可以在表单中使用它:

Public Class Form1
    Dim myRefresher As Refresher = New Refresher()

    Private Sub tsbGo_Click(sender As Object, e As EventArgs) Handles tsbGo.Click
        WebBrowser1.Navigate(txtUrl.Text)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        myRefresher.Browser = WebBrowser1
    End Sub

    Private Sub txtMin_TextChanged(sender As Object, e As EventArgs) Handles txtMin.TextChanged
        Dim int As Integer = 0
        If Integer.TryParse(txtMin.Text, int) Then
            myRefresher.MinimumWait = int
        End If
    End Sub

    Private Sub txtMax_TextChanged(sender As Object, e As EventArgs) Handles txtMax.TextChanged
        Dim int As Integer = 0
        If Integer.TryParse(txtMax.Text, int) Then
            myRefresher.MaximumWait = int
        End If
    End Sub
End Class