ContextSwitchDeadlock occurred

I am writing a parser that takes a fixed length data text file and converts it to a CSV file, for the requirement of a process.
The text file is 12MB and I am getting a the non pumping message "... The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages."

I am reading the text file, writing it to a datatable before saving it to a CSV file.  A code extract is below.  
I have tried calling the DoEvents(), but it doesn't seem to be doing anything.

Any help would be much appreciated.

Thank you
Ahsinah


        For Each file As String In files

            'strTxt = IO.File.ReadAllText(file)
            Dim allLines As String() = IO.File.ReadAllLines(file)
            For Each line As String In allLines

                CLECLIENT = line.Substring(0, 1).TrimEnd()
                NOCLIENT = line.Substring(1, 8).TrimEnd()
                CIVILITE = line.Substring(9, 38).TrimEnd()
                NOM = line.Substring(47, 38).TrimEnd()
                PRENOM = line.Substring(85, 38).TrimEnd()
                VOLET1 = line.Substring(123, 38).TrimEnd()
                VOLET2 = line.Substring(161, 38).TrimEnd()
                VOLET3 = line.Substring(199, 38).TrimEnd()
                VOLET4 = line.Substring(237, 38).TrimEnd()
                VOLET5 = line.Substring(275, 38).TrimEnd()
                VOLET6 = line.Substring(313, 38).TrimEnd()
                CODE_POSTAL = line.Substring(351, 5).TrimEnd()
                COMMUNE = line.Substring(356, 38).TrimEnd()
                LIBELLE = line.Substring(394, 30).TrimEnd()
                IBAN = line.Substring(424, 27).TrimEnd()
                CODE_PAYS = line.Substring(424, 2).TrimEnd()    ' 2 characters from IBAN field itself
                BIC = line.Substring(451, 11).TrimEnd()
                TELEPHONE = line.Substring(462, 10).TrimEnd()
                NUM_TEL_FIXE = line.Substring(472, 10).TrimEnd()
                NUM_TEL_PORTABLE = line.Substring(482, 10).TrimEnd()
                DATE_NAISS = line.Substring(492, 8).TrimEnd()
                EMAIL = line.Substring(500, 100).TrimEnd()
                Q_CUIS_GASTRO = line.Substring(600, 1).TrimEnd()
                Q_DECO = line.Substring(601, 1).TrimEnd()
                Q_JARDIN = line.Substring(602, 1).TrimEnd()
                Q_VOYAGE = line.Substring(603, 1).TrimEnd()
                Q_SANTE = line.Substring(604, 1).TrimEnd()
                Q_HISTOIRE = line.Substring(605, 1).TrimEnd()
                Q_FAMILLE = line.Substring(606, 1).TrimEnd()
                FID_MARKETING = line.Substring(607, 1).TrimEnd()

                dt.Rows.Add(CLECLIENT, NOCLIENT, CIVILITE, NOM, PRENOM, VOLET1, VOLET2, VOLET3, VOLET4,
                    VOLET5, VOLET6, CODE_POSTAL, COMMUNE, LIBELLE, IBAN, CODE_PAYS, BIC, TELEPHONE,
                    NUM_TEL_FIXE, NUM_TEL_PORTABLE, DATE_NAISS, EMAIL, Q_CUIS_GASTRO, Q_DECO, Q_JARDIN,
                    Q_VOYAGE, Q_SANTE, Q_HISTOIRE, Q_FAMILLE, FID_MARKETING)

                Application.DoEvents()
            Next

        Next

        DataGridView1.DataSource = dt

        Dim csv As String = String.Empty
        Dim i As Integer = 0


       For Each column As DataGridViewColumn In DataGridView1.Columns
            csv += column.HeaderText & ","c
        Next
        csv += vbCr & vbLf

        For Each row As DataGridViewRow In DataGridView1.Rows
            csv += row.Cells.Item(0).Value & ","
            csv += row.Cells.Item(1).Value & ","
            csv += row.Cells.Item(2).Value & ","
            csv += row.Cells.Item(3).Value & ","
            csv += row.Cells.Item(4).Value & ","
            csv += row.Cells.Item(5).Value & ","
            csv += row.Cells.Item(6).Value & ","
            csv += row.Cells.Item(7).Value & ","
            csv += row.Cells.Item(8).Value & ","
            csv += row.Cells.Item(9).Value & ","
            csv += row.Cells.Item(10).Value & ","
            csv += row.Cells.Item(11).Value & ","
            csv += row.Cells.Item(12).Value & ","
            csv += row.Cells.Item(13).Value & ","
            csv += row.Cells.Item(14).Value & ","
            csv += row.Cells.Item(15).Value & ","
            csv += row.Cells.Item(16).Value & ","
            csv += row.Cells.Item(17).Value & ","
            csv += row.Cells.Item(18).Value & ","
            csv += row.Cells.Item(19).Value & ","
            csv += row.Cells.Item(20).Value & ","
            csv += row.Cells.Item(21).Value & ","
            csv += row.Cells.Item(22).Value & ","
            csv += row.Cells.Item(23).Value & ","
            csv += row.Cells.Item(24).Value & ","
            csv += row.Cells.Item(25).Value & ","
            csv += row.Cells.Item(26).Value & ","
            csv += row.Cells.Item(27).Value & ","
            csv += row.Cells.Item(28).Value & ","
            csv += row.Cells.Item(29).Value


            csv += vbCr & vbLf
        Next

        Dim folderPath As String = "F:\work\UNI\Import\"
        File.WriteAllText(folderPath & "DataGridViewExport.csv", csv)

        Label1.Visible = True
        Label1.Text = "Fichier CSV sauvegardé"
ahsinah maderTechnical Support EngineerAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Add a BackgroundWorker() component to your form.  In the DoWork() handler, add the "meat" of your code (the long running LOOPS).  In the RunWorkerCompleted() event, update the Label and the DataSource.  ...also, you can build your "csv" variable using the DataTable directly instead of reading from the grid.  Get rid of DoEvents().
0
 
ahsinah maderTechnical Support EngineerAuthor Commented:
Thanks Mike. Your guidance was spot on!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.