1

我正在做一个项目,该项目需要我对外部设备生成的 5 个独立事件做出反应,然后在一定延迟后执行某些操作。这些事件通常一次发生一个,但有时可能同时发生。这是一个坏主意,如果是这样,为什么?

Imports System.Windows.Threading
Class MainWindow

Private TimerList As List(Of DispatcherTimer)

Private Sub Window_Loaded(ByVal sender As System.Object, 
ByVal e As ystem.Windows.RoutedEventArgs) Handles MyBase.Loaded

TimerList = New List(Of DispatcherTimer)

    'Create 5 timers for the 5 events from external device
    For i As Integer = 1 To 5
        TimerList.Add(
            New DispatcherTimer(TimeSpan.FromSeconds(10), DispatcherPriority.Normal,
                            New System.EventHandler(AddressOf tmr_Tick),
                            Me.Dispatcher) With {.Tag = i}
                        )
    Next

End Sub

Public Sub SomeEventFromExternalDevice(ByVal ID As Integer) 'handles...

    Dim CurTimer = TimerList.Find(Function(x) x.Tag = ID)

    If Not CurTimer Is Nothing Then
        CurTimer.Start()
    End If

End Sub

Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim ID = DirectCast(sender.tag, Integer)

    Dim curTimer = TimerList.Find(Function(x) x.Tag = ID)

    curTimer.Stop()

    Select Case ID
        'change something on the UI to indicate that event x timer has elapsed
    End Select

End Sub

End Class 
4

1 回答 1

1

可能更好的解决方案是完全摆脱 DispatcherTimer 并使用普通线程执行此操作。

首先创建一个类来保存单个事件工作包 - 我在此处添加了一种传递延迟值的方法,但如果它不符合您的需要,您可以省略它。这是一个简单的过程,它使线程休眠延迟,然后引发一个新事件供您捕获。如果您需要精度,那么您可以使用秒表或其他东西来实现延迟:

Public Class DelayThread
    ' Task information
    Private _ID As Integer
    Private _delay As Integer
    Event onDelayUp(ByVal ID As Integer)

    ' Constructor
    Public Sub New(ByVal myID As Integer, ByVal myDelay As Integer)
        _ID = myID
        _delay = myDelay
    End Sub

    Public Sub DelayProc()
        System.Threading.Thread.Sleep(_delay)
        RaiseEvent onDelayUp(_ID)
    End Sub
End Class

现在您的设备将触发此处处理的事件:

Public Sub SomeEventFromExtDevice(ByVal ID As Integer) 'Handles ...
        'get a "delay" value from somewhere, if you like
        Dim newDelayTask As New DelayThread(ID, delay)  'added delay here

        AddHandler newDelayTask.onDelayUp, AddressOf DoSomething

        Dim t As New System.Threading.Thread( _
        New System.Threading.ThreadStart(AddressOf newDelayTask.DelayProc))

        t.Priority = Threading.ThreadPriority.Normal 'whatever priority
        t.Start()

End Sub

这样,每次触发事件时,您都会启动一个新线程,该线程等待您的延迟时间,执行 DoSomething 然后终止,在进程中自行清理。

在这里,您将需要一些“DoSomething”程序:

 'declare this somewhere
 Delegate Sub dlgDoSomething(ByVal ID As Integer)

 Public Sub DoSomething(ByVal ID As Integer) 'hooked to onDelayUp @ runtime above
      'using "Me" here - if DoSomething is somewhere else 
      'you may need to refer to the main UI form instead  
      Me.Invoke(New dlgDoSomething(AddressOf uiDoSomething), New Object() {ID})
 End Sub

DoSomething 过程将从每个线程调用,因此它必须在主 UI 线程上调用 - 然后需要:

Public Sub uiDoSomething(ByVal ID As Integer)
        ' Do Something with ID - UI thread is now executing this so you are safe
End Sub

如果知道事件的确切顺序很重要——知道它们何时到达以及以什么顺序到达——那么您可以在 SomeEventFromExtDevice 中添加一个时间戳并将其传递。

您可能还想为关闭应用程序添加一些处理 - 例如,这里没有检查以确保线程在处理后不会尝试编组到主窗体上。

于 2012-01-06T16:30:58.603 回答