Option Explicit On
Option Strict On
Imports System.ComponentModel
Public Class Form1
Private ReceivedBarcodes As New Queue(Of String)
Private MessagesToSend As New Queue(Of String)
Private WithEvents CheckTimer As New Timer
Private WithEvents bwBarcodeProcessor As New BackgroundWorker
Private WithEvents bwMessageSender As New BackgroundWorker
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'setup the timer
With CheckTimer
.Interval = 50 'run every 50ms
.Start()
End With
'setup the backgroundworker bwBarcodeProcessor
bwBarcodeProcessor.WorkerReportsProgress = False
'setup the backgroundworker bwMessageSender
bwMessageSender.WorkerReportsProgress = False
'for testing
ReceivedBarcodes.Enqueue("00000001")
ReceivedBarcodes.Enqueue("00000002")
ReceivedBarcodes.Enqueue("00000003")
End Sub
Private OldMessageToRemove As String = String.Empty
Private Sub CheckTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckTimer.Tick
If ReceivedBarcodes.Count > 0 And bwBarcodeProcessor.IsBusy = False Then
'there is a barcode to process, run the backgroundworker
bwBarcodeProcessor.RunWorkerAsync()
End If
If MessagesToSend.Count > 0 AndAlso bwMessageSender.IsBusy = False Then
'there is a message to send, run the backgroundworker
bwMessageSender.RunWorkerAsync()
End If
End Sub
Private ReceivedBarcode As String = String.Empty
Private Sub bwBarcodeProcessor_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwBarcodeProcessor.DoWork
While ReceivedBarcodes.Count > 0
'keep repeating until the queue is empty
ReceivedBarcode = ReceivedBarcodes.Dequeue
ShowMessage(String.Format("Received Barcode: {0}", ReceivedBarcode))
'TODO: process your Received Barcode here
'your select and update SQL queries would all go here
'when finished processing, add the response to send into the queue
MessagesToSend.Enqueue("ResponseMessage related to " & ReceivedBarcode)
End While
End Sub
Private Sub bwBarcodeProcessor_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bwBarcodeProcessor.RunWorkerCompleted
'currently no more messages to process
End Sub
Private MessageToSend As String = String.Empty
Private Sub bwMessageSender_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwMessageSender.DoWork
While MessagesToSend.Count > 0
'keep repeating until the queue is empty
MessageToSend = MessagesToSend.Dequeue
'TODO: send your message in reply here
ShowMessage(String.Format("Sent Message: {0}", MessageToSend))
End While
End Sub
Private Sub bwMessageSender_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bwMessageSender.RunWorkerCompleted
'currently no more outgoing messages to send
End Sub
Delegate Sub ShowMessageInvoker(ByVal Message As String)
Public Sub ShowMessage(ByVal Message As String)
If txtMessages.InvokeRequired Then
'invoke required
txtMessages.Invoke(New ShowMessageInvoker(AddressOf ShowMessage), New Object() {Message})
Else
'add the processed message to the text box
txtMessages.Text &= Message & Environment.NewLine
End If
End Sub
Private RandomBarcodeGenerator As New Random()
Private RandomBarcode As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'for testing purposes ONLY !!
RandomBarcode = RandomBarcodeGenerator.Next(1, 10000000)
ReceivedBarcodes.Enqueue(String.Format("{0:000000000}", RandomBarcode))
End Sub
End Class