Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Double update from threaded code

Status
Not open for further replies.

bakershawnm

Programmer
Joined
Apr 18, 2007
Messages
84
Location
US
Not sure if this is because of threading or what.

Using VS2008 VB.net and SQL server 2005.

I have the following module inside a thread class. The thread is being spawned by a controlling form.

Private Sub BuildBatTag(ByVal infl As StreamReader)
Dim instrng As String
Dim BCAOPScnxn As New ADODB.Connection
Dim BtTgdata As New ADODB.Recordset
Dim x As Integer

BCAOPScnxn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;" & _
"Data Source=KRONOS;Initial Catalog=BCA_Operations"
BCAOPScnxn.Open()

With BtTgdata
.Open("dbo.BatTagData2", BCAOPScnxn, ADODB.CursorTypeEnum.adOpenDynamic, _
ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdTable)
.AddNew()
.Fields("Model").Value = Model
.Fields("Assembly_Part_No").Value = AssyNo
.Fields("Wir_No").Value = WirNo
.Fields("AP_No").Value = Eff
.Fields("ImprtDate").Value = Today()
For x = 1 To 8
instrng = infl.readline()
Select Case x
Case 2
.Fields("Wire_Bundle").Value = Mid(instrng, 2, 14)
.Fields("DCN").Value = Mid(instrng, 16, 2)
Case 5
.Fields("Remarks").Value = Mid(instrng, 62, 20)
Case 6
.Fields("Used_On").Value = Left(instrng, 20)
Case 8
.Fields("Using_Shop").Value = Mid(instrng, 9, 3)
End Select
Next
.Update()
RaiseEvent Prgrsbar(3)
.Close()
End With
BCAOPScnxn.Close()
BCAOPScnxn = Nothing

End Sub

When I step through the code the with a trap at the .Update it will execute this statement (and any subsequent statements and probably previous statements too) twice.

If this is a threading issue how can I solve this? Is it possible that my form is somehow spawning more than one thread?

If it is not a threading issue than what would cause the double execution of the same statements?

 
Idea:
I am wondering is it this code that is running twice, or it is getting called twice? If the code is called from only a few places maybe you should log (to a simple text file ) the calling code/form and see what happens.

Also consider logging the thread ID (??AppDomain.GetCurrentThreadId??) to see if there is two threads or the same thread running twice.

I know, not the clean/clear answer you wanted, but I hope it is of some limited help.

BTW I am trying to improve my VB.Net skills and one of the ways I do is by reading other's code. I was looking and trying to understand the code provided in this question. Could you explain to me the benifts to having the loop please? I might of written the code as shown below (This replaces the For Next code )
Code:
          instrng = infl.readline() ‘Throw away the first line

          instrng = infl.readline() ‘2nd line
          .Fields("Wire_Bundle").Value = Mid(instrng, 2, 14)
          .Fields("DCN").Value = Mid(instrng, 16, 2)

          instrng = infl.readline() ‘Throw away the 3rd line
          instrng = infl.readline() ‘Throw away the 4th line

          instrng = infl.readline() ‘5th line
         .Fields("Remarks").Value = Mid(instrng, 62, 20)

          instrng = infl.readline() ‘6th line
          .Fields("Used_On").Value = Left(instrng, 20)

          instrng = infl.readline() ‘Throw away the 7’th line

          instrng = infl.readline() ‘8th line
          .Fields("Using_Shop").Value = Mid(instrng, 9, 3)

The other question I have is the use of the 'Left()' and 'Mid()' functions. Why those instead of the .Substring() method? In fact I do not understand the use of the Left() method at all as it does not (normally, my understanding) take any parameters and returns an integer type. What magic is happening? Could this be VB 6 code? (I have not done any VB 6 coding)

Lion Crest Software Services
Anthony L. Testi
President
 
actually your answer was very helpful. The calling form was spawning the thread twice. I had been playing with wait handles and thread queue instead of just thread.start and missed a couple of spots when I put it back to thread.start.

To answer your question about left/mid -v- substring I am an old VBA programmer and substring is relatively new to me. so my mind is used to using them and I have not retrained myself to use substring. So yes the Left/Mid/Right come from previous version of VB. It wasn't until SQL Server programming (where there is no Mid) that I learned about the substring.

Also Left does use one parameter as does Right. It is the number of characters you wish to grab from the string.

The reason for the loop is to shorten the amount of code. I learned a long time ago to look for ways to make my code as short as possible. I think it had something to do with the more statements in the code the larger the executable and back then small executables were important. I learned to do pc programming on old 8088 and 80286 machines with a max of 640k of ram.

Thanks for the help
 
Glad to be of help.
" I learned to do pc programming on old 8088 and 80286 machines with a max of 640k of ram. "

My first computer was a hand wire wrapped 6502 based computer with 16k, I upgraded to a 48K Apple ][, later maxed it out with 64K. A 640K 8088 was my 4th or 5th computer.

"I learned a long time ago to look for ways to make my code as short as possible."

Now a days short code might have only a 0.0001% effect on the performace/size of an exe. Modern compilers change the code anyway, for example for optimization reasons many compliers will unroll a small loop like (1 to 8 ) into straight line code for speed reasons (kind of like what I did by hand). Suggest do not write for small code, write for readablity, maintenance etc. (Since you 'like' the loop style I am sure it is readable for you in the end.)

Lion Crest Software Services
Anthony L. Testi
President
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top