Results 1 to 2 of 2

Thread: Dataset issues... Possible Coss-thread operation not valid... Need some help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2007
    Posts
    190

    Dataset issues... Possible Coss-thread operation not valid... Need some help

    I am writing an application that is doing a file conversion.

    I created a new process in order to run an exe and pass it some arguments in order to do the file conversion. I ran this in the Submit button click event.

    I added an event handler for when the Process had exited and then continued my code, since I had to wait for the file conversion to complete before continuing.

    Everything is running fine now up until the point where I am trying to call a FillBy for one of my datasets. My fill by is accepting a parameter UserID and filling it based on the user id, which is selected in a combo box in the program itself.

    I keep getting exceptions and it immediately exits the program whenever I try and use this fillby method, if I take it out it completes successfully; however, the new row I just inserted was not added.

    I tried adding a tryparse for the value selected from the UserID combobox but I get this cross-thread exception, so I have a feeling the 2 are related.

    Any ideas what I should do? Here is what I have thus far:
    My btn click event
    Code:
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            If txtFileDesc.Text <> String.Empty Or txtFileDesc.Text <> "" Then
                If File.Exists(txtFileLoc.Text) Then
                    Dim extension As String
                    Dim extensionPos As Integer
                    Dim filePos As Integer
    
                    extension = txtFileLoc.Text
                    extensionPos = extension.LastIndexOf(".")
                    extension = extension.Remove(0, extensionPos)
    
                    kmlFileName = txtFileLoc.Text.Replace(extension, ".kml")
                    kmzFileName = txtFileLoc.Text.Replace(extension, ".kmz")
                    shpFileName = txtFileLoc.Text.Replace(extension, ".shp")
    
                    filePos = txtFileLoc.Text.LastIndexOf("\")
                    filePos = filePos + 1
                    fileName = txtFileLoc.Text.Remove(0, filePos)
    
                    If extension = ".kmz" Then
                        Upload()
                        UpdateDS()
    
                    ElseIf extension = ".kml" Then
                        If System.IO.File.Exists(kmzFileName) Then
                            If MessageBox.Show(String.Format("The file you have specified {" + kmzFileName + "} already exists.  Do you want to overwrite this file?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) = DialogResult.Yes Then
                                System.IO.File.Delete(kmlFileName)
                            Else
                                Return
                            End If
                        End If
                        GenerateZipFile()
    
                    ElseIf extension = ".shp" Then
                        ToolStripStatusLabel.Text = statusKML
                        Dim ogrDirectory As String
                        ogrDirectory = Application.StartupPath + "\ogr"
                        ogrDirectory = "C:\Program Files (x86)\EMTRAC\User GISMap Management Application Install\ogr\"
    
                        With conversionProc
                            .EnableRaisingEvents = True
                            .StartInfo.WorkingDirectory = ogrDirectory
                            .StartInfo.FileName = "ogr2ogr.exe"
                            .StartInfo.Arguments = "-f KML " + kmlFileName + " " + shpFileName
                            .StartInfo.WindowStyle = ProcessWindowStyle.Hidden
                        End With
                        conversionProc.Start()
                    Else
                        Return
                    End If
    
                End If
            End If
        End Sub
    My Code to generate a zip file:
    Code:
    Sub GenerateZipFile()
            ToolStripStatusLabel.Text = statusZip
            Dim zip As New ZipFile
            zip.AddItem(kmlFileName)
            zip.Save(kmzFileName)
            kmzCreated = True
    
            Upload()
            UpdateDS()
    
        End Sub
    My Code to Upload the zipped file:
    Code:
    Sub Upload()
            ToolStripStatusLabel.Text = statusUpload
            Dim uploadFileURL As String
            Dim uploadClient As New System.Net.WebClient
    
            uploadFileURL = ftpURL + fileName
    
            uploadClient.Credentials = New System.Net.NetworkCredential("ommitted", "ommitted")
    
            uploadClient.UploadFile(uploadFileURL, kmzFileName)
    
            uploadFileName = uploadFileURL.Replace("ftp://ftp", "http://www")
    
            CleanUp()
        End Sub
    My code to update my dataset
    Code:
    Sub UpdateDS()
            Dim sqlConnection As New System.Data.SqlClient.SqlConnection()
            Dim strCommand As String
    
            sqlConnection.ConnectionString = My.Settings.EMTRACKMLConnectionString
            strCommand = "INSERT INTO KMLUSERFILES (USER_ID, FILE_DESCRIPTION, FILE_LOCATION) VALUES (@userID, @fileDesc, @fileLocation)"
    
            Dim sqlCommand As New System.Data.SqlClient.SqlCommand(strCommand, sqlConnection)
            sqlCommand.Parameters.AddWithValue("@userID", UserID)
            sqlCommand.Parameters.AddWithValue("@fileDesc", txtFileDesc.Text)
            sqlCommand.Parameters.AddWithValue("@fileLocation", uploadFileName)
    
            sqlConnection.Open()
            sqlCommand.ExecuteNonQuery()
            sqlConnection.Close()
    
            Dim tValue As Boolean
            Dim tUserID As Long
    
            tValue = Long.TryParse(cmbUserID.SelectedText, tUserID)
            If tValue = True Then
                Me.KMLUSERFILESTableAdapter.FillBy(Me.EMTRACKMLDataSet.KMLUSERFILES, tUserID)
            End If
        End Sub
    My Code where I declare the process:
    Code:
    Private WithEvents conversionProc As New System.Diagnostics.Process
    My Code where I handle the Exit event of the process
    Code:
    Private Sub conversionProc_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles conversionProc.Exited
    
            kmlCreated = True
    
            GenerateZipFile()
        End Sub
    I have tried just skipping reading the value from the combo box for the userid as I have it stored already through the code. But I end up with the same error happening giving me something about Index 0 is not valid or something along those lines. I have values already in my table, the index starts at 0 and increments automatically by 1. I have the start seed at 0 and the increment set for 1 for the Index in my data set. Any ideas?

  2. #2
    Addicted Member
    Join Date
    Mar 2008
    Posts
    143

    Re: Dataset issues... Possible Coss-thread operation not valid... Need some help

    Try using Invoke on the GenerateZipFile() method.
    Shut up and eat your banana!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width