|
-
Nov 6th, 2015, 05:24 AM
#1
Thread Starter
Member
Jump between loops
Hi all I'm in need of some help which I'm sure is very easy (but not to me). I need to read 2 text files and act on them independently. The first file is acted upon and data is sent to the top half of a form the second needs to be acted upon and sent to the bottom half text boxes etc.
So my for loop can read the first text file and deal with the data but how do I (at the same time) read the second file and act on that line by line.
By the way the form is real time Information which has time data and location data from 2 different sources and therefore can't be read one at a time.
If you need any of the For Loop code I will post it but basically
For Second Run of loop
Read Text File A Read Text File A
Line 1 of A Line 2 Of A
Handle A Data Handle A Data
Read Text File B Read Text File B
Read Text File B Read Text File B
Line 1 of B Line 2 of B
Handle B Data Handle B Data
Loop Loop
-
Nov 6th, 2015, 07:08 AM
#2
Fanatic Member
Re: Jump between loops
Hello
Maybe you should post your code. I am a bit confused. That being said. Won't using two loops work? One for TEXTFILE A and one for TEXTFILE B?
George
-
Nov 6th, 2015, 07:27 AM
#3
Thread Starter
Member
Re: Jump between loops
Hi George the For loop is a tad long if I run a loop TEXTFILE A Line A I then need to jump out and read TEXTFILE B Line A then back to TEXTFILE A and so on as they are GPS timed lines and the data needs to be handled for each file separately
Just noticed what I typed earlier it didn't pan out as I typed it
Read text file A
Read Text File B
For each line in FileA
line of A
Handle data
Jump to File B Loop read line of file B
Next
For each line in FileB
line of B
Handle data
Jump to File A Loop read Next Line of File
Next
Does this make any sense ?
-
Nov 6th, 2015, 07:38 AM
#4
Re: Jump between loops
Code:
Open FileA
Open FileB
Start looping
Read from FileA
Do something
Read from FileB
Do something with that
keep looping until the end of FileA or FileB
Close FileA
Close FileB
Seems pretty straight forward to me... same basic structure you posted... sooooo...... ?????????
-tg
-
Nov 6th, 2015, 08:59 AM
#5
Thread Starter
Member
Re: Jump between loops
Ok so here is my 2 For loops which I need to run at the same time Merge them somehow
Code:
For Each line1 As String In File.ReadLines(LocalFileName)
If MyLocalExit = False Then
If Pause = False Then
If line1.Contains("$GPRMC") Then
Dim valueLine() As String = line1.Split(",")
' MsgBox(valueLine(7))
LocalMapLat = valueLine(7).Substring(0, 2) & "°"
LocalMapLat = LocalMapLat & valueLine(7).Substring(2) & """"
LocalMapLat = LocalMapLat & valueLine(8)
LocalMapLatMinutes = (CSng(Mid(valueLine(7), 3, 7)) / 60)
LocalMapsngLatitude = CSng(VB.Left(valueLine(7), 2)) + LocalMapLatMinutes
LocalMapLat = CStr(LocalMapsngLatitude)
LocalMapLatDecimal = CDbl(Val(LocalMapsngLatitude))
LocalMapLng = valueLine(9).Substring(0, 3) & "°"
LocalMapLng = LocalMapLng & valueLine(9).Substring(3) & """"
LocalMapLng = LocalMapLng & valueLine(10)
LocalMapLngMinutes = (CSng(Mid(valueLine(9), 4, 7)) / 60)
LocalMapsngLongitude = CSng(VB.Left(valueLine(9), 3)) + LocalMapLngMinutes
LocalMapLng = CStr(LocalMapsngLongitude)
LocalMapLngDecimal = CDbl(Val(LocalMapsngLongitude))
Dim MyLocalDate = valueLine(1).Substring(0, 10)
TextBox4.Text = MyLocalDate
Dim MyLocalTime = valueLine(2).Substring(0, 12)
TextBox5.Text = MyLocalTime
LocalMapsngLongitude = (LocalMapsngLongitude * -1)
tLoc = tMap.GetLocation(LocalMapLatDecimal, LocalMapLngDecimal, 0.0)
tLoc.GoTo()
TextBox2.Text = ("Latitude " & LocalMapsngLatitude & " Longitude " & LocalMapsngLongitude)
Car = tMap.AddPushpin(tLoc)
Car.Symbol = 30
Car.BalloonState = GeoBalloonState.geoDisplayNone
Wait(100)
End If
If line1.Contains("Score") Then
ScoreCCount = ScoreCCount + 1
TextBox9.Text = ScoreCCount
Dim valueLine() As String = line1.Split(",")
Dim Score = valueLine(4).Substring(35, 4)
Dim OffSet = valueLine(5).Substring(0, 32)
TextBox3.Text = (OffSet)
Dim MyScore = valueLine(4).Substring(11, 28)
TextBox6.Text = MyScore
Dim ScoreFail = valueLine(4).Substring(35, 5)
If ScoreFail <= 2.499 Then
ScoreFailCount = ScoreFailCount + 1
TextBox11.Text = ScoreFailCount
I = I + 1
result = (ScoreFailCount / I) * 100
TextBox10.Text = result & (" %")
End If
If ScoreFail >= 2.5 Then
I = I + 1
result = (ScoreFailCount / I) * 100
TextBox10.Text = result & (" %")
End If
tLoc = tMap.GetLocation(LocalMapLatDecimal, LocalMapLngDecimal, 0.0)
tLoc.GoTo()
If Score < 2.5 Then
Smiley = tMap.AddPushpin(tLoc)
Smiley.Symbol = 65
Smiley.BalloonState = GeoBalloonState.geoDisplayNone
Else
Smiley = tMap.AddPushpin(tLoc)
Smiley.Symbol = 70
Smiley.BalloonState = GeoBalloonState.geoDisplayNone
End If
Smiley.Note = "Score " & Score
If CheckBox2.Checked = True Then
Smiley.BalloonState = GeoBalloonState.geoDisplayBalloon
Else
Smiley.BalloonState = GeoBalloonState.geoDisplayNone
End If
End If
If line1.Contains("HW On") Then
OvalShape1.BackColor = Color.Red
ElseIf line1.Contains("HW Off") Then
OvalShape1.BackColor = Color.Lime
End If
If line1.Contains("BEGIN") Then
OvalShape2.BackColor = Color.Yellow
ElseIf line1.Contains("END") Then
OvalShape2.BackColor = Color.Lime
End If
Dim MyWait As Integer
MyWait = ComboBox1.SelectedItem
Wait(MyWait)
' System.Threading.Thread.Sleep(1000)
System.Windows.Forms.Application.DoEvents()
If line1.Contains("Com") Then
Dim valueLine() As String = line1.Split(",")
Dim ComPort = valueLine(0).Substring(14, 2)
TextBox7.Text = ComPort
End If
If line1.Contains("HW") Then
Dim valueLine() As String = line1.Split(",")
Dim RADPort = valueLine(0).Substring(14, 2)
TextBox8.Text = HWPort
End If
End If
End If
Next line1
System.Threading.Thread.Sleep(1000)
System.Windows.Forms.Application.DoEvents()
For Each line2 As String In File.ReadLines(DistantFileName)
If MyDistantExit = False Then
If Pause = False Then
If line2.Contains("$GPRMC") Then
Dim valueLine() As String = line2.Split(",")
' MsgBox(valueLine(7))
DistantMapLat = valueLine(7).Substring(0, 2) & "°"
DistantMapLat = DistantMapLat & valueLine(7).Substring(2) & """"
DistantMapLat = DistantMapLat & valueLine(8)
DistantMapLatMinutes = (CSng(Mid(valueLine(7), 3, 7)) / 60)
DistantMapsngLatitude = CSng(VB.Left(valueLine(7), 2)) + DistantMapLatMinutes
DistantMapLat = CStr(DistantMapsngLatitude)
DistantMapLatDecimal = CDbl(Val(DistantMapsngLatitude))
DistantMapLng = valueLine(9).Substring(0, 3) & "°"
DistantMapLng = DistantMapLng & valueLine(9).Substring(3) & """"
DistantMapLng = DistantMapLng & valueLine(10)
DistantMapLngMinutes = (CSng(Mid(valueLine(9), 4, 7)) / 60)
DistantMapsngLongitude = CSng(VB.Left(valueLine(9), 3)) + DistantMapLngMinutes
DistantMapLng = CStr(DistantMapsngLongitude)
DistantMapLngDecimal = CDbl(Val(DistantMapsngLongitude))
Dim MyDistantDate = valueLine(1).Substring(0, 10)
TextBox17.Text = MyDistantDate
Dim MyDistantTime = valueLine(2).Substring(0, 12)
TextBox16.Text = MyDistantTime
Wait(100)
DistantMapsngLongitude = (DistantMapsngLongitude * -1)
TextBox14.Text = ("Latitude " & DistantMapsngLatitude & " Longitude " & DistantMapsngLongitude)
End If
If line2.Contains("Score") Then
ScoreCCount = ScoreCCount + 1
TextBox22.Text = ScoreCCount
Dim valueLine() As String = line2.Split(",")
Dim Score = valueLine(4).Substring(35, 4)
Dim OffSet = valueLine(5).Substring(0, 32)
TextBox18.Text = (OffSet)
Dim MyScore = valueLine(4).Substring(11, 28)
TextBox15.Text = MyScore
Dim ScoreFail = valueLine(4).Substring(35, 5)
If ScoreFail <= 2.499 Then
ScoreFailCount = ScoreFailCount + 1
TextBox20.Text = ScoreFailCount
I = I + 1
result = (ScoreFailCount / I) * 100
TextBox21.Text = result & (" %")
End If
If ScoreFail >= 2.5 Then
I = I + 1
result = (ScoreFailCount / I) * 100
TextBox21.Text = result & (" %")
End If
End If
If line2.Contains("HW On") Then
OvalShape4.BackColor = Color.Red
ElseIf line2.Contains("HW Off") Then
OvalShape4.BackColor = Color.Lime
End If
If line2.Contains("BEGIN") Then
OvalShape3.BackColor = Color.Yellow
ElseIf line2.Contains("END") Then
OvalShape3.BackColor = Color.Lime
End If
System.Windows.Forms.Application.DoEvents()
If line2.Contains("Com") Then
Dim valueLine() As String = line2.Split(",")
Dim ComPort = valueLine(0).Substring(14, 2)
TextBox19.Text = ComPort
End If
End If
End If
Next line2
-
Nov 6th, 2015, 11:29 AM
#6
Re: Jump between loops
Let me see if I understand what you want. Your code's one big chunk, so I could've missed what's going on.
It looks like you want to search two different files for some data. The data may be in either file, and you aren't really sure which one it is. As soon as the data is found in either file, you want to do some things and stop searching?
At a high level, that is not hard. Starting two asynchronous tasks with cancellation has several patterns. But the way your code is currently structured, it needs a lot of work to get there. You update a lot of controls in response to this data, and that's dangerous when multiple threads are involved.
So I would restructure it as an asynchronous function that returns some data, then update the UI based on that data. That way you can make the searching, which is probably the part that takes a long time, happen asynchronously and do the relatively quick updates on the UI thread in response. The newer task-based APIs would be super convenient for this, but aren't quite as well-supported in VS 2010. You should consider upgrading to VS 2013 (my favorite) or VS 2015, unless you have a strong technical reason to use VS 2010.
Now, if you really want both to run at the same time, just be allowed to finish at different times, you still need to do the same thing. Other threads cannot update the UI, and things would get messy if they could. If you make the changes discussed above, you won't have that problem.
So let's make sure we understand the question first, because coordinating multiple threads is always a little tricky. I don't want to write a lot of example code and explanation about the wrong thing.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Nov 6th, 2015, 11:38 AM
#7
Thread Starter
Member
Re: Jump between loops
Ok may have not explained very well
It looks like you want to search two different files for some data. The data may be in either file and you aren't really sure which one it is, Yes I do know the first loop refers to the data in the first file the second loop refers to the data in the second file. As soon as the data is found in either file, you want to do some things and stop searching?no I stop at the end of the file its a time sequence of events
At a high level, that is not hard. Starting two asynchronous tasks with cancellation has several patterns. But the way your code is currently structured, it needs a lot of work to get there. You update a lot of controls in response to this data, and that's dangerous when multiple threads are involved. This is because it is mapping a vehicle with lat long positions so they change every few lines
So I would restructure it as an asynchronous function that returns some data, then update the UI based on that data. That way you can make the searching, which is probably the part that takes a long time, happen asynchronously and do the relatively quick updates on the UI thread in response. The newer task-based APIs would be super convenient for this, but aren't quite as well-supported in VS 2010. You should consider upgrading to VS 2013 (my favorite) or VS 2015, unless you have a strong technical reason to use VS 2010.
Now, if you really want both to run at the same time, just be allowed to finish at different times, you still need to do the same thing. Other threads cannot update the UI, and things would get messy if they could. If you make the changes discussed above, you won't have that problem.
So let's make sure we understand the question first, because coordinating multiple threads is always a little tricky. I don't want to write a lot of example code and explanation about the wrong thing.
-
Nov 6th, 2015, 01:01 PM
#8
Re: Jump between loops
Well basically what you are asking is just not correct. You can not jump in an out of loops back and forth nor do you need to. Post #4 seems to show the structure you need.
-
Nov 6th, 2015, 02:09 PM
#9
Re: Jump between loops
OK. Interesting. It sounds like you are in the case techgnome talked about in #4. You could use multiple threads, but I think the costs of coordinating those threads are worse than the simplicity of Techgnome's approach.
Just to clarify, this is what you want, right?
Code:
* Read from file A.
* Update the UI.
* Read from file B.
* Update the UI.
* Repeat until all data has been read.
I can only help with general algorithms in this topic, for contractual reasons I'd rather not discuss.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|