ZeroEffect
Junior Contributor
I am trying to implement a delay when send data. Because of this delay I will need to queue any data received while waiting for the delay to expire. I have everything set up I am using System.collections Queue for holding the data received while the delay is active. Once the delay has expired I check the queue and then process that data. I have all of this working. To create the delay I am processsing the data in a new thread then inside that thread I am using that thread sleep function. This works too but while this thread is sleeping then main thread sleeps too.
here is my test code.
Data from this application is sent correctly it is delayed 7 seconds for each item in the queue. but durring this time the main application is frozen.
Any thoughts?
Thanks
ZeroEffect
yes I will be switching to sockets when move away from the test application.
here is my test code.
Visual Basic:
Imports System.Threading
Imports System.Collections
Public Class Form1
Private Delegate Sub tbCall(ByVal tb As TextBox)
Dim rp1Q As Queue
Dim tRP1 As Thread
Dim blntRP1 As Boolean = False
Shared Sub Main()
Application.Run(New Form1())
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
rp1Q = New Queue(10)
wsRecData.Bind()
End Sub
Private Sub wsRecData_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles wsRecData.DataArrival
Dim strData As Object
wsRecData.GetData(strData)
TextBox2.AppendText(Now & vbTab & BuildString(strData) & vbCrLf)
ProcessData(BuildString(strData))
End Sub
Private Function BuildString(ByVal strData As Array) As String
Dim temp As Long, mString As String
mString = ""
For temp = 0 To UBound(strData)
mString &= Chr(strData(temp))
Next
Return mString
End Function
Private Sub ProcessData(ByVal tempData As String)
tempData = Replace(tempData, "|", "") 'Remove Pipe
tempData = Replace(tempData, "^", "") 'Remove Carot
'add to cue and start processing
'if data is to be forwarded.
'if port is enabled cue data and start processing.
rp1Q.Enqueue(tempData)
If blntRP1 = False Then
tRP1 = New Thread(AddressOf processRP1)
tRP1.IsBackground = True
tRP1.Start()
End If
End Sub
Private Sub processRP1()
Dim tempData As String
blntRP1 = True
If TextBox1.InvokeRequired Then
Dim LogsafeSub As New tbCall(AddressOf processRP1)
TextBox1.Invoke(LogsafeSub, TextBox1)
Else
Do While rp1Q.Count > 0
'pause if there is a delay
tRP1.Sleep(7000)
tempData = rp1Q.Dequeue()
wsSendData.SendData(Now & vbTab & tempData & vbCrLf)
Loop
blntRP1 = False
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Clear()
TextBox2.Clear()
End Sub
End Class
Data from this application is sent correctly it is delayed 7 seconds for each item in the queue. but durring this time the main application is frozen.
Any thoughts?
Thanks
ZeroEffect
yes I will be switching to sockets when move away from the test application.