Context Switch Deadlock was detected

My VB code below zips up images in a folder and moves the zipped files to another folder.. I'm getting the Contect Switch Deadlock Was detected error message and then when I click continue I can see in the black executable box that it starts running out of memory.. How can I fix this? I'm a SQL developer NOT a VB person and we currently do not have a VB person working here.. I did not build this code.. I do know a little but will need you to explain in detail what I should do..
Imports System.IO.Packaging
Imports System.IO
Imports System.Text
Imports System.Configuration


Module ZipModule

    Dim zip As Package
    'Dim zipPath As String = "J:\eDreamz\DataLoads\ZipFiles\"
    Dim zipPath As String = "\\lx98sqlp1\f$\eDreamz\DataLoads\ZipFiles\"
    'Dim zipPath As String = "D:\eDreamz\DataLoads\ZipFiles\"
    'Dim zipPath As String = "C:\Program Files\CreateZipFile-EDreamz\ZipFiles\"

    Dim zipName As String
    'Dim zipDelete As String = "J:\eDreamz\DataLoads\ZipFiles"
    Dim zipDelete As String = "\\lx98sqlp1\f$\eDreamz\DataLoads\ZipFiles"
    'Dim zipDelete As String = "D:\eDreamz\DataLoads\ZipFiles"
    'Dim zipDelete As String = "C:\Program Files\CreateZipFile-EDreamz\ZipFiles"


    Sub main()
        'Delete existing zip files....
        Console.WriteLine("Delete ZipFiles")
        DeleteZipFile()

        'zipName = "Thumbnail Images New"
        'ZipFiles()
        zipName = "Detail Images"
        ZipFiles()
        'zipName = "Zoom Images New"
        'ZipFiles()
        Console.WriteLine("End of Zip Process")

    End Sub

    Private Sub ZipFiles()

        Dim zipFullPath As String

        zipFullPath = zipPath + zipName + ".zip"

        Console.WriteLine("Write zip file: " & zipFullPath.ToString)

        'Open the zip file if it exists, else create a new one 
        'Dim zip As Package
        zip = ZipPackage.Open(zipFullPath, _
             IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)

        'Add as many files as you like:
        FindFilesTest(zipName)

        zip.Close() 'Close the zip file

    End Sub

    Private Sub FindFilesTest(ByVal zipX As String)
        'Dim HP_Location = "G:\E-dreamz Images\Catalog Cover JPGs"
        'Dim HP_Location = "G:\E-dreamz Images\" + zipX
        Dim HP_Location = "\\lex98fs1\infoshare$\E-dreamz Images\" + zipX
        Dim filesToAdd As String

        Dim di As New System.IO.DirectoryInfo(HP_Location)
        Try

            For Each fi As System.IO.FileInfo In di.GetFiles
                'If fi.Name.ToUpper.EndsWith(UserName) Then
                'copy files
                'fi.CopyTo(System.IO.Path.Combine(JobsLocation, fi.Name), True)
                'fi.CopyTo(System.IO.Path.Combine(ArchiveLocation, fi.Name), True)

                'delete file
                'fi.Delete()

                'collect filenames (see the count below the loop)
                'FilesArray.Add(fi.Name)
                'End If
                filesToAdd = HP_Location + "\" + fi.Name

                Console.WriteLine("Write: " & filesToAdd.ToString)

                AddToArchive(zip, filesToAdd)

            Next
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            'MsgBox("Error creating zip: " & ex.Message)
        End Try

        'MessageBox.Show(FilesArray.Count, "# Files Processed")

    End Sub

    Private Sub AddToArchive(ByVal zip As Package, _
                         ByVal fileToAdd As String)
        Try


            'Replace spaces with an underscore (_) 
            Dim uriFileName As String
            uriFileName = fileToAdd.ToString

            'A Uri always starts with a forward slash "/" 
            Dim zipUri As String = String.Concat("/", _
                       IO.Path.GetFileName(uriFileName))

            Dim partUri As New Uri(zipUri, UriKind.Relative)
            Dim contentType As String = _
                       Net.Mime.MediaTypeNames.Application.Zip

            'The PackagePart contains the information: 
            ' Where to extract the file when it's extracted (partUri) 
            ' The type of content stream (MIME type):  (contentType) 
            ' The type of compression:  (CompressionOption.Normal)   
            Dim pkgPart As PackagePart = zip.CreatePart(partUri, _
                       contentType, CompressionOption.Normal)

            'Read all of the bytes from the file to add to the zip file 
            Dim bites As Byte() = File.ReadAllBytes(fileToAdd)

            'Compress and write the bytes to the zip file 
            pkgPart.GetStream().Write(bites, 0, bites.Length)

        Catch ex As Exception
            Console.WriteLine(ex.Message)
            'MsgBox("Add to Zip File error: " & ex.Message)
        End Try

    End Sub

    Private Sub DeleteZipFile()

        Dim di As New System.IO.DirectoryInfo(zipDelete)
        For Each fi As System.IO.FileInfo In di.GetFiles
            'delete files
            fi.Delete()

        Next


    End Sub

End Module

Open in new window

LVL 1
cheryl9063Asked:
Who is Participating?
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.

Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, mmmm, this stream (line #122 of your example):
'Compress and write the bytes to the zip file 
pkgPart.GetStream().Write(bites, 0, bites.Length)

Open in new window


I think there is your problem... change that line to this:
'Compress and write the bytes to the zip file 
Using myStream As System.IO.Stream = pkgPart.GetStream()
    myStream.Write(bites, 0, bites.Length)
End Using

Open in new window

0
cheryl9063Author Commented:
Still getting

The CLR has been unable to transition from COM context 0x4dce30 to COM context 0x4dcfa0 for 60 seconds. 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. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.
0
Carlos VillegasFull Stack .NET DeveloperCommented:
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
cheryl9063Author Commented:
You da bomb
0
cheryl9063Author Commented:
For some reason I thought I awarded points already for this.. Sorry about that
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.

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.