Context Switch Deadlock was detected

Posted on 2011-10-12
Last Modified: 2012-05-12
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")

        'zipName = "Thumbnail Images New"
        zipName = "Detail Images"
        'zipName = "Zoom Images New"
        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:

        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)

            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

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

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

                AddToArchive(zip, filesToAdd)

        Catch ex As Exception
            '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)

            '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("/", _

            Dim partUri As New Uri(zipUri, UriKind.Relative)
            Dim contentType As String = _

            '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
            '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


    End Sub

End Module

Open in new window

Question by:cheryl9063
    LVL 17

    Expert Comment

    by:Carlos Villegas
    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

    LVL 1

    Author Comment

    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.
    LVL 17

    Accepted Solution

    LVL 1

    Author Comment

    You da bomb
    LVL 1

    Author Closing Comment

    For some reason I thought I awarded points already for this.. Sorry about that

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now