"Jason Sobell (iGadget)" <iGadget_@hotmail.com> wrote
[Snip]
> Perhaps you should code a solution to this problem? It would be a good
> exercise for us both (after all, I might be wrong).
> Since the main strength of OO development is reuse, and you seem confident
> that you can develop a solution quickly, please post a sample solution to
> this thread. I will do the same thing using a HashTable.
> As the OP stated, we have a list of nodes (we can ignore this detail), and
> he wants to prevent the user clicking on any node more often than, say,
> 5secs. We could also prevent the user clicking unresponsive nodes more

than
> every 10secs? That would be a nice sensible feature that shows the
> flexibility of a solution.
> So we have 2 functions required:
> 1. Public Function Can_We_Send(MachineName as String) As Boolean
> 2. Public Sub Response_Received(MachineName as String)


OK, so here is a solution using a HashTable:

------------
Public Class NodeHitTimout
Private myHashTable As New Hashtable()
Public Function Can_We_Send(ByVal MachineName As String) As Boolean
If Not myHashTable.ContainsKey(MachineName) Then
myHashTable.Add(MachineName, New TimeEntry())
Return True
Else
Dim timeentry As TimeEntry = CType(myHashTable.Item(MachineName), _
TimeEntry)
Dim howlongago As Long = DateDiff(DateInterval.Second, _
timeentry.TimeLastClicked, System.DateTime.Now())
If howlongago < 5 Then Return False
If howlongago < 10 And Not timeentry.Responded Then Return False
myHashTable.Item(MachineName) = New TimeEntry()
Return True
End If
End Function

Public Sub Response_Received(ByVal MachineName As String)
If myHashTable.ContainsKey(MachineName) Then
CType(myHashTable.Item(MachineName), TimeEntry).Responded = True
End If
End Sub

Public Class TimeEntry
Public TimeLastClicked As Date = System.DateTime.Now()
Public Responded As Boolean = False
End Class
End Class
------------

Note that this is a _complete_ solution to the problem, and not just a
section.
If you would like to try it, I've attached a sample test harness below.
Ugly, but
functional

Brandon, I'm not sure if this is still a requirement for you, but if it is,
hopefully this code might be helpful.

Cheers,
Jason



----- Long and horrible VB.NET Form code follows:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents Button6 As System.Windows.Forms.Button
Friend WithEvents Button10 As System.Windows.Forms.Button
Friend WithEvents Button9 As System.Windows.Forms.Button
Friend WithEvents Button8 As System.Windows.Forms.Button
Friend WithEvents Button7 As System.Windows.Forms.Button
Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
Friend WithEvents Button5 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.GroupBox1 = New System.Windows.Forms.GroupBox()
Me.Button10 = New System.Windows.Forms.Button()
Me.Button9 = New System.Windows.Forms.Button()
Me.Button8 = New System.Windows.Forms.Button()
Me.Button7 = New System.Windows.Forms.Button()
Me.Button6 = New System.Windows.Forms.Button()
Me.GroupBox2 = New System.Windows.Forms.GroupBox()
Me.Button5 = New System.Windows.Forms.Button()
Me.Button4 = New System.Windows.Forms.Button()
Me.Button3 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.Button1 = New System.Windows.Forms.Button()
Me.GroupBox1.SuspendLayout()
Me.GroupBox2.SuspendLayout()
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.Location = New System.Drawing.Point(8, 198)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(376, 264)
Me.ListBox1.TabIndex = 5
'
'GroupBox1
'
Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control()
{Me.Button10, Me.Button9, Me.Button8, Me.Button7, Me.Button6})
Me.GroupBox1.Location = New System.Drawing.Point(120, 8)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(112, 176)
Me.GroupBox1.TabIndex = 11
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Acknowledges"
'
'Button10
'
Me.Button10.Location = New System.Drawing.Point(16, 144)
Me.Button10.Name = "Button10"
Me.Button10.Size = New System.Drawing.Size(72, 23)
Me.Button10.TabIndex = 14
Me.Button10.Text = "Machine5"
'
'Button9
'
Me.Button9.Location = New System.Drawing.Point(16, 112)
Me.Button9.Name = "Button9"
Me.Button9.Size = New System.Drawing.Size(72, 23)
Me.Button9.TabIndex = 13
Me.Button9.Text = "Machine4"
'
'Button8
'
Me.Button8.Location = New System.Drawing.Point(16, 80)
Me.Button8.Name = "Button8"
Me.Button8.Size = New System.Drawing.Size(72, 23)
Me.Button8.TabIndex = 12
Me.Button8.Text = "Machine3"
'
'Button7
'
Me.Button7.Location = New System.Drawing.Point(16, 48)
Me.Button7.Name = "Button7"
Me.Button7.Size = New System.Drawing.Size(72, 23)
Me.Button7.TabIndex = 11
Me.Button7.Text = "Machine2"
'
'Button6
'
Me.Button6.Location = New System.Drawing.Point(16, 16)
Me.Button6.Name = "Button6"
Me.Button6.Size = New System.Drawing.Size(72, 23)
Me.Button6.TabIndex = 7
Me.Button6.Text = "Machine1"
'
'GroupBox2
'
Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control()
{Me.Button5, Me.Button4, Me.Button3, Me.Button2, Me.Button1})
Me.GroupBox2.Location = New System.Drawing.Point(8, 8)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(104, 176)
Me.GroupBox2.TabIndex = 12
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "Request"
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(16, 144)
Me.Button5.Name = "Button5"
Me.Button5.TabIndex = 9
Me.Button5.Text = "Machine5"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(16, 112)
Me.Button4.Name = "Button4"
Me.Button4.TabIndex = 8
Me.Button4.Text = "Machine4"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(16, 80)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 7
Me.Button3.Text = "Machine3"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(16, 48)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 6
Me.Button2.Text = "Machine2"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(16, 16)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 5
Me.Button1.Text = "Machine1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(392, 469)
Me.Controls.AddRange(New System.Windows.Forms.Control()
{Me.GroupBox2, Me.GroupBox1, Me.ListBox1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox2.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region

Private HT As NodeHitTimout = New NodeHitTimout()

Private Sub ButtonSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click,
Button4.Click, Button5.Click
Dim B As Button = CType(sender, Button)
If Not HT.Can_We_Send(B.Text) Then
ListBox1.Items.Insert(0, "Timeout pending for " + B.Text)
Else
ListBox1.Items.Insert(0, "Request sent to " + B.Text)
End If
End Sub

Private Sub ButtonAck_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button6.Click, Button7.Click, Button8.Click,
Button9.Click, Button10.Click
Dim B As Button = CType(sender, Button)
HT.Response_Received(B.Text)
End Sub

End Class