ZeroEffect Posted April 22, 2009 Posted April 22, 2009 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. 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. Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
ZeroEffect Posted April 23, 2009 Author Posted April 23, 2009 Re: delay sending data *update* So for the heck of it I added this line to my project to see what happened. Control.CheckForIllegalCrossThreadCalls = False and everything worked like I wanted it to. It looks like my issue has to due with haveing to use delagates and the [controlname].invoke for cross thread access to the controls and when the thread sleeps the whole thing sleeps. I know it is not a good coding to not check for these cross thread calls does anyone have a better solution? Thanks for your help, ZeroEffect Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
ZeroEffect Posted April 27, 2009 Author Posted April 27, 2009 Re: delay sending data *update* Any thoughts on this? Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
Nate Bross Posted April 27, 2009 Posted April 27, 2009 This code If TextBox1.InvokeRequired Then Dim LogsafeSub As New tbCall(AddressOf processRP1) TextBox1.Invoke(LogsafeSub, TextBox1) Else Is making the sub processRP1 execute on the same thread that TextBox1 was created (the UI thread). Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
ZeroEffect Posted September 23, 2009 Author Posted September 23, 2009 After many months of other projects I am able to com back to this. I was able to solve my problem by using a background worker. Now I have another seemingly sime issue and this has to do with time. I need to delay the data "x" amount of time from when it is received. sound simple. eh? let say I have a delay of 7 seconds converted to milliseconds (7000) data is received at 00:00:20 data out at 00:00:27 if any data is received durring the delay time it is added to a cue to be processed after the delayed data is over. In a perfect world there should be no data receieved durring the delay. if any data came in between 00:00:20 and 00:00:27 the delay would be 7 seconds from the time it was received and not 7 second from the last time data was sent. I am looking for suggestions for getting this delay and coverting it into the correct milliseconds. I was thinking (#seconds to delay - ([time in] - [current time])) * 1000) First event might look like this (7 - (00:00:20 - 00:00:20) ) * 1000 (7 - 0) * 1000 7 * 1000 = 7000 I should not be doing math when I am tired LOL Thanks for your thoughts. ZeroEffect Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.