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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ahsinah maderTechnical Support EngineerAuthor Commented:
Thanks Mike. Your guidance was spot on!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.