Ot,
Then to add to the queue, ActionQueue.Enqueue(DelaySeconds,AddressOf Sub1,
Parm1, Parm2) and also ActionQueue.Enqueue(DelaySeconds,AddressOf Sub2,
parm_1, parm_2, parm_3). That is, the passed routines may have different
parameter lists. The DelaySeconds is the wait required before executing
the passed routine.
Its unclear by your example. Are the parameters set when you enqueue the
item or when you dequeue the item?
3 choices that I would consider include, but are not limited to:
1) Define 2 Delegates one for 2 parameters & 1 for 3 parameters, then use
the TypeOf Is operator to decide which delegate to invoke.
Public Delegate Sub AnyRoutine1(ByVal parm1 As String, ByVal parm2 As
Integer)
Public Delegate Sub AnyRoutine2(ByVal parm1 As String, ByVal parm2 As
Integer, ByVal parm3 As Double)
Public Shared Sub Routine1(ByVal parm1 As String, ByVal parm2 As
Integer)
Debug.WriteLine(parm1, "routine1")
End Sub
Public Shared Sub Routine2(ByVal parm1 As String, ByVal parm2 As
Integer, ByVal parm3 As Double)
Debug.WriteLine(parm1, "routine2")
End Sub
Public Class ActionQueue
Private ReadOnly m_queue As New Queue
Public ReadOnly Property IsEmpty() As Boolean
Get
Return m_queue.Count = 0
End Get
End Property
Public Sub Enqueue(ByVal routine As AnyRoutine1)
m_queue.Enqueue(routine)
End Sub
Public Sub Enqueue(ByVal routine As AnyRoutine2)
m_queue.Enqueue(routine)
End Sub
Public Sub Process(ByVal parm1 As String, ByVal parm2 As Integer,
ByVal parm3 As Double)
Dim value As Object = m_queue.Dequeue()
If TypeOf value Is AnyRoutine1 Then
Dim routine As AnyRoutine1 = DirectCast(value, AnyRoutine1)
routine(parm1, parm2)
ElseIf TypeOf value Is AnyRoutine2 Then
Dim routine As AnyRoutine2 = DirectCast(value, AnyRoutine2)
routine(parm1, parm2, parm3)
End If
End Sub
End Class
Public Shared Sub Main()
Dim q As New ActionQueue
q.Enqueue(AddressOf Routine1)
q.Enqueue(AddressOf Routine2)
Do Until q.IsEmpty
q.Process("a", 1, 2)
Loop
End Sub
2) Use a class derived from an "argument" class such as EventArgs. The
Delegate would accept the "EventArgs" class, each "handler" would need to
check the type of EventArgs class to see number of parameters...
3) Define an action/command class that is placed in the queue, Specific
"functions" would derive from the base action/command class. This would be a
variation of the Command Pattern.
Public MustInherit Class Action
Public MustOverride Sub Routine()
End Class
Public Class Action1
Inherits Action
Public Sub New(ByVal parm1 As String, ByVal parm2 As Integer)
End Sub
Public Overrides Sub Routine()
End Sub
End Class
Public Class Action2
Inherits Action
Public Sub New(ByVal parm1 As String, ByVal parm2 As Integer, ByVal
parm3 As Double)
End Sub
Public Overrides Sub Routine()
End Sub
End Class
Public Class ActionQueue
Private ReadOnly m_queue As New Queue
Public ReadOnly Property IsEmpty() As Boolean
Get
Return m_queue.Count = 0
End Get
End Property
Public Sub Enqueue(ByVal routine As Action)
m_queue.Enqueue(routine)
End Sub
Public Sub Enqueue(ByVal parm1 As String, ByVal parm2 As Integer)
m_queue.Enqueue(New Action1(parm1, parm2))
End Sub
Public Sub Enqueue(ByVal parm1 As String, ByVal parm2 As Integer,
ByVal parm3 As Double)
m_queue.Enqueue(New Action2(parm1, parm2, parm3))
End Sub
Public Sub Process()
Dim value As Action = DirectCast(m_queue.Dequeue(), Action)
value.Routine()
End Sub
End Class
Public Shared Sub Main()
Dim q As New ActionQueue
Dim parm1 As String, parm2 As Integer, parm3 As Double
q.Enqueue(New Action1(parm1, parm2))
q.Enqueue(New Action2(parm1, parm2, parm3))
' alternatives
q.Enqueue(parm1, parm2)
q.Enqueue(parm1, parm2, parm3)
Do Until q.IsEmpty
q.Process()
Loop
End Sub
The first two have "polymorphism problems", in that they require you to
inquire of the type of delegate or type of parameter being passed. Using an
array or ParamArray would be a good 4th method. It really depends on what
other things are going on in the ActionQueue class...
Hope this helps
Jay