Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Context Switch Deadlock was detected

Posted on 2011-10-12
Medium Priority
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
  • 3
  • 2
LVL 17

Expert Comment

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


Author Comment

ID: 36958770
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

Carlos Villegas earned 2000 total points
ID: 36958824

Author Comment

ID: 36958960
You da bomb

Author Closing Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

564 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