Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27193

[RESOLVED] ContextSwitchDeadlock was detected

$
0
0
So I've never ran into this error before. The title is: ContextSwitchDeadlock was detected. The messagebody is:
Quote:

The CLR has been unable to transition from COM context 0x9370d8 to COM context 0x937248 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
it allowed me to continue running the code, but it happened on a very long processing task, which I'm sure I'm going to have to bring over to a bgworker. The code I'm using is:
Code:

If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            f_name = ofd.FileName

            'Start the xml reader
            Dim reader As Xml.XmlReader = Xml.XmlReader.Create(f_name)

            'Get the x and y ubounds for 2d array
            Dim x_max As Integer = 0
            Dim y_max As Integer = 0

            'read
            While reader.Read
                If reader.IsStartElement() Then
                    'Get the current x and y
                    Dim currentx As Integer = CInt(reader("LocationX"))
                    Dim currenty As Integer = CInt(reader("LocationY"))

                    'If they're higher than the current ubounds
                    'then set the ubounds to the current x and y
                    If currentx > x_max Then x_max = currentx
                    If currenty > y_max Then y_max = currenty

                End If
            End While

            're-read
            Dim newreader As Xml.XmlReader = Xml.XmlReader.Create(f_name)

            'ReDim with new ubounds
            ReDim board_level1(x_max, y_max)
            While newreader.Read
                If newreader.IsStartElement() Then
                    'Get the attributes:
                    'ID, IsPassable, ImageLocation, and X/Y Coordinates
                    Dim id As Integer = CInt(newreader("ID"))
                    Dim ispassable As Boolean
                    If newreader("IsPassable") = "True" Then
                        ispassable = True
                    Else
                        ispassable = False
                    End If
                    Dim imglocation As String = newreader("ImageLocation")
                    Dim currentx As Integer = CInt(newreader("LocationX"))
                    Dim currenty As Integer = CInt(newreader("LocationY"))

                    'the x/y 2d array
                    board_level1(currentx, currenty) = New Map_Tile

                    'Set it's properties
                    With board_level1(currentx, currenty)
                        .ID = id
                        .IsPassable = ispassable
                        .ImageLocation = imglocation
                        .Location = New Point(currentx * 50, currenty * 50)
                    End With

                    'Add it to the Panel
                    SplitContainer1.Panel2.Controls.Add(board_level1(currentx, currenty))

                End If
            End While

        End If

Basically it's reading a map file and storing it into a 2d array. My questions are:
  1. Is there anything I can do to speed this puppy up?
  2. Should it be placed in a backgroundworker?
  3. What should I do prevent the error from coming up again?


Thanks for all y'all's help.

Viewing all articles
Browse latest Browse all 27193

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>