每10秒运行一次的vb.net队列



我有一个每10秒运行一次的队列:

Private Sub Form1Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Dim t As New Timers.Timer(10000)
        AddHandler t.Elapsed, New ElapsedEventHandler(AddressOf Elapsed)
        t.Start()
    End Sub
    Private Sub Elapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs)
        Dim timer1 As Timers.Timer = DirectCast(sender, Timers.Timer)
        Try
            ' disable temporarily
            timer1.Stop()
            _quemanager.StartProcessing()
            Thread.Sleep(1000)
        Finally
            ' make sure that the handler is always reenables
            timer1.Start()
        End Try
    End Sub
Namespace Managers
    Public Class QueueManager
        Private ReadOnly _items As New Queue
        Public Sub StartProcessing()
            For Each x In From i In File.GetAllAccountFolders() From x1 In File.CheckFolderForFiles(i & "In") Select x1
                _items.Enqueue(x)
            Next
            Dim t1 As New Threading.Thread(AddressOf Process)
            t1.Start()
        End Sub
        Private Sub Process()
            Do
                SyncLock _items.SyncRoot
                    If _items.Count = 0 Then
                        'No more items.
                        Logger.Log("QueueManager", "Process", "No Items In Queue")
                        Exit Do
                    End If
                    Logger.Log("QueueManager", "Process", "Processing File: " & _items.Peek)
                    FileProcessingManager.ProcessFile(_items.Peek)
                    _items.Dequeue()
                End SyncLock
            Loop
        End Sub
    End Class
End Namespace

这背后的逻辑应该是,计时器每10秒一次,停止计时器运行队列,然后当队列完成时,它应该再次启动计时器?在队列结束之前,计时器不可能自行重新启动?

感谢

在示例代码中,QueueManager. StartProcessing启动一个新线程,用于处理队列中的项目。这意味着它将立即返回,可能在队列处理之前。然后,在计时器运行事件处理程序中,在延迟一秒钟后重新启动计时器。因此,如果队列处理时间超过1秒,那么在队列处理完成之前,计时器确实会重新启用。在这种情况下,我会完全取消1秒睡眠,并在完成后让QueueManager引发一个事件。然后,您的窗体可以只监视该事件,当它被引发时,它可以在事件处理程序中重新启用计时器。不过,在触摸计时器的属性之前,您需要先执行Me.Invoke以返回UI线程。

Public Class Form1
    Private WithEvents _quemanager As New QueueManager()
    Private WithEvents _t As New Timers.Timer(10000)
    Private Sub Form1Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        t.Start()
    End Sub
    Private Sub _t_Elapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs) Handles _t.Elapsed
        ' disable temporarily
        _t.Stop()
        _quemanager.StartProcessing()
    End Sub
    Private Sub _quemanager_ProcessingCompleted() Handles _quemanager.ProcessingCompleted
        _t.Start()
    End Sub
End Class
Public Class QueueManager
    Private ReadOnly _items As New Queue
    Public Event ProcessingCompleted()
    Public Sub StartProcessing()
        '...
    End Sub
    Private Sub Process()
        Do
            '...
        Loop
        RaiseEvent ProcessingCompleted()
    End Sub
End Class

最新更新