【问题标题】:Visual Basic .NET: ScheduleVisual Basic .NET:时间表
【发布时间】:2011-01-05 20:06:06
【问题描述】:

是否有可能在我的 Visual Basic .NET 表单中检查每隔 X 间隔执行一个函数?

【问题讨论】:

    标签: vb.net winforms timer schedule


    【解决方案1】:

    查看Timer 类。

    Public Class Form1
        Private T As Timer
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            T = New Timer()
            AddHandler T.Tick, AddressOf TimerTicker
            T.Interval = (1000 * 3) 'Every 3 seonds
            T.Start()
        End Sub
        Private Sub TimerTicker(ByVal sender As Object, ByVal ev As EventArgs)
            Trace.WriteLine("here")
        End Sub
    End Class
    

    【讨论】:

      【解决方案2】:

      你是说在某个时间间隔运行一个函数吗?如果是这样,那么 Timer 控件将起作用。一个快速的Google search 会给你一些关于定时器的教程。

      【讨论】:

        【解决方案3】:

        这个怎么样:使用计时器,然后用任何你想要的方法替换 MessageBox 警报。

        以下示例实现了一个简单的间隔计时器,它每五秒触发一次警报。当警报发生时,MessageBox 会显示警报启动的次数,并提示用户计时器是否应该继续运行。

        您可以找到更多详细信息here

         Public Class Class1
        >     Private Shared WithEvents myTimer As New System.Windows.Forms.Timer()
        >     Private Shared alarmCounter As Integer = 1
        >     Private Shared exitFlag As Boolean = False    
        
        > 
        >     ' This is the method to run when the timer is raised.
        >     Private Shared Sub TimerEventProcessor(myObject As
        > Object, _
        >                                            ByVal myEventArgs As EventArgs) _
        >                                        Handles myTimer.Tick
        >         myTimer.Stop()
        > 
        >         ' Displays a message box asking whether to continue running the
        > timer.
        >         If MessageBox.Show("Continue running?", "Count is: " &
        > alarmCounter, _
        >                             MessageBoxButtons.YesNo) =
        > DialogResult.Yes Then
        >             ' Restarts the timer and increments the counter.
        >             alarmCounter += 1
        >             myTimer.Enabled = True
        >         Else
        >             ' Stops the timer.
        >             exitFlag = True
        >         End If
        >     End Sub
        > 
        >     Public Shared Sub Main()
        >         ' Adds the event and the event handler for the method that will
        >         ' process the timer event to the timer.
        > 
        >         ' Sets the timer interval to 5 seconds.
        >         myTimer.Interval = 5000
        >         myTimer.Start()
        > 
        >         ' Runs the timer, and raises the event.
        >         While exitFlag = False
        >             ' Processes all the events in the queue.
        >             Application.DoEvents()
        >         End While
        > 
        >     End Sub    
        > 
        > End Class
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-30
          • 2016-06-03
          • 2013-08-16
          • 1970-01-01
          相关资源
          最近更新 更多