Question Background Worker Cross Thread Operation on Active MDI Child

pidyok

Member
Joined
Feb 10, 2009
Messages
19
Programming Experience
Beginner
If anybody could help me please how to put this on a background worker without getting cross thread errors??? trying to upload a childform object values to an access db... I don't work in IT, I'm just another employee working to make things easier in the office, so my apologies for asking here...

Our company's LAN so slow that it takes around 3~5 seconds trying to update/insert values in access using the code below... so putting it on a background worker i think makes the app immediately usable once save/upload's called... unless otherwise somebody could suggest a faster way to speed things up... thanks!!!

VB.NET:
 Sub iUploadNew()

        Dim iDateTimeNow As Date = Now
        Dim iCap As Integer, iCoP As Integer, iCoord As Integer, iCopt As Integer
        Dim iDateTimeX As Date, iXY As Integer

        iDateTimeX = iActiveChild.lblDate.Text & " " & Mid(iActiveChild.lblTime.Text, 1, 2) & ":" & Mid(iActiveChild.lblTime.Text, 3, 2) & ":00"
        Try
            connStr = "SELECT * FROM lstPilots WHERE PilotName='" & iActiveChild.lblCaptain.Text & "'"
            iStrAdapter()
            iCap = dTable1.Rows(0).Item(0)
            connStr = "SELECT * FROM lstPilots WHERE PilotName='" & iActiveChild.lblCoPilot.Text & "'"
            iStrAdapter()
            iCoP = dTable1.Rows(0).Item(0)
            connStr = "SELECT * FROM lstCoordinators WHERE CoordinatorName='" & iActiveChild.lblCoordinator.Text & "'"
            iStrAdapter()
            iCoord = dTable1.Rows(0).Item(0)
            connStr = "SELECT * FROM lstCopters WHERE CopterCallSign='" & iActiveChild.lblCallSign.Text & "'"
            iStrAdapter()
            iCopt = dTable1.Rows(0).Item(0)
        Catch ex As Exception
            MsgBox("Check entries for sysntax errors...", MsgBoxStyle.Exclamation, "Error creating manifest...")
            Exit Sub
        End Try
        dTable1.Dispose()

        If iActiveChild.txtManID.Text = 0 Then
            connStr = "INSERT INTO tblManDetails (ManTimeStamp,ManDateTime,ManCoordinatorID," & _
                "ManCaptainID,ManCoPilotID,ManCopterID,ManBoardingCard,ManRoute,ManFuel,ManWeight,ManTemp,ManCustomerID) " & _
                "VALUES(#" & iDateTimeNow & "#,#" & iDateTimeX & "#," & iCoord & "," & _
                iCap & "," & iCoP & "," & iCopt & ",'" & iActiveChild.lblColor.Text & "','" & _
                iActiveChild.cboRoute.Text & "'," & iActiveChild.lblFuel.Text & "," & _
                iActiveChild.lblWeight.Text & "," & iActiveChild.lblTemp.Text & "," & _
                Trim(Replace(Mid(iActiveChild.txtShowOptima.Text, 1, 2), "_", "", 1)) & ")"
            iStrInsert()

            connStr = "SELECT * FROM tblManDetails WHERE ManTimeStamp=#" & iDateTimeNow & "#"
            iStrAdapter()

            iManID = dTable1.Rows(0).Item(0)
            connStr = "INSERT INTO tblManPax (ManID) VALUES (" & iManID & ")"
            iStrInsert()

            connStr = "INSERT INTO tblManFrt (ManID) VALUES (" & iManID & ")"
            iStrInsert()

            connMain.Close()
            dTable1.Dispose()

            iActiveChild.txtManID.Text = iManID
        End If

        iManID = iActiveChild.txtManID.Text
        connStr = "UPDATE tblManDetails SET ManDateTime=#" & iDateTimeX & "#,ManCoordinatorID=" & _
            iCoord & ",ManCaptainID=" & iCap & ",ManCoPilotID=" & iCoP & ",ManCopterID=" & iCopt & _
            ",ManBoardingCard='" & iActiveChild.lblColor.Text & "',ManRoute='" & iActiveChild.cboRoute.Text & _
            "',ManFuel=" & iActiveChild.lblFuel.Text & ",ManWeight=" & _
            iActiveChild.lblWeight.Text & ",ManTemp=" & iActiveChild.lblTemp.Text & " WHERE ManID=" & iManID
        iStrInsert()

        For iXY = 0 To iActiveChild.lsv1.Items.Count - 1
            connStr = "UPDATE tblManPax SET ManPax" & iActiveChild.lsv1.Items(iXY).SubItems(0).Text & _
            "='" & iActiveChild.lsv1.Items(iXY).SubItems(0).Text & "," & _
            iActiveChild.lsv1.Items(iXY).SubItems(1).Text & "," & _
            iActiveChild.lsv1.Items(iXY).SubItems(2).Text & "," & _
            iActiveChild.lsv1.Items(iXY).SubItems(3).Text & "," & _
            iActiveChild.lsv1.Items(iXY).SubItems(4).Text & "," & _
            iActiveChild.lsv1.Items(iXY).SubItems(5).Text & "," & _
            iActiveChild.lsv1.Items(iXY).SubItems(6).Text & "," & _
            iActiveChild.lsv1.Items(iXY).SubItems(7).Text & "' WHERE ManID=" & iManID
            iStrInsert()
        Next

        For iXY = 0 To iActiveChild.lvwCargo.Items.Count - 1
            connStr = "UPDATE tblManFrt SET ManFrt" & iXY + 1 & _
            "='" & iActiveChild.lvwCargo.Items(iXY).SubItems(0).Text & "," & _
            iActiveChild.lvwCargo.Items(iXY).SubItems(1).Text & "," & _
            iActiveChild.lvwCargo.Items(iXY).SubItems(2).Text & "," & _
            iActiveChild.lvwCargo.Items(iXY).SubItems(3).Text & "," & _
            iActiveChild.lvwCargo.Items(iXY).SubItems(4).Text & "," & _
            iActiveChild.lvwCargo.Items(iXY).SubItems(5).Text & "' WHERE ManID=" & iManID
            iStrInsert()
        Next

        If iXY < 12 Then
            iXY = iXY + 1
            For iXY = iXY To 12
                connStr = "UPDATE tblManFrt SET ManFrt" & iXY & _
                "='' WHERE ManID=" & iManID
                iStrInsert()
            Next
        End If
        dTable1.Dispose()
    End Sub
 
I've seen some shoddy coding in my time, but that probably set a new record; i'd replace the guy that wrote it first, then either have the app rewritten by someone who knew what they were doing as a consultancy work or hire someone who knew what they were doing

Rewrite the app to use SQLServer and modern, sensible programming practices
 

Latest posts

Back
Top