none
ContextSwitchDeadlock was detected

    Question

  • Hi

    I am getting below error on a long running query crashing the app, even though I am running the query as background task;

    ContextSwitchDeadlock was detected
    Message: The CLR has been unable to transition from COM context 0x623978 to COM context 0x623ae8 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.

    The code is below. How can I fix the 'ContextSwitchDeadlock was detected' issue please?

        Function ExecuteSQL(ByVal St As String) As Boolean
    
            Try
    
                Try
    
                    Dim result = ExecuteSQLAsync(DBCommand)
                    I = result.Result
    
                Catch ae As AggregateException
    
                    For Each ex In ae.InnerExceptions
                        Throw
                    Next
    
                End Try
    
                Return True
    
            Catch ex As SystemException
    
                MsgBox(ex.Message)
    
                Return True
    
            End Try
    
        End Function
    
    
        Async Function ExecuteSQLAsync(DBCommand As SqlCommand) As Task(Of Integer)
    
            Dim x = Await DBCommand.ExecuteNonQueryAsync()
    
            Return x
    
        End Function

    Thanks

    Regards


    Thursday, June 19, 2014 12:25 PM

All replies

  • Hello,

    What happens when running normally using synchronous methods rather than asynchronous. I am asking because it sounds like an issue with the database side rather than code.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


    Thursday, June 19, 2014 12:55 PM
  • Hi

    This issue was the reason that I went to running it async in the first place. Previously it was running normally but still giving this error.

    Thanks

    Regards

    Thursday, June 19, 2014 1:23 PM
  • Using Async/Await does not help with the database side, only the User Interface.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, June 19, 2014 2:33 PM
  • http://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?category=sqlserver

    You may want to post to the above forum to the SQL Server Service Broker section, becase SB can run long running queries, and you can tell SB what to do through VB code, becuase SB can run CLR based solutions. 

    Thursday, June 19, 2014 3:14 PM
  • Y a h y a

    I you like I can move this question to the SQL Server Service Broker section.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, June 19, 2014 3:32 PM
  • Hi

    That would be great. Thanks.

    Regards

    Thursday, June 19, 2014 3:49 PM
  • Hello,

    Thank you for your question. I am trying to involve someone more familiar with this topic for a further look at this issue. Sometime delay might be expected from the job transferring. Your patience is greatly appreciated. 

    Thank you for your understanding and support.

    If you have any feedback on our support, please click here.

    Regards,


    Elvis Long
    TechNet Community Support

    Monday, June 23, 2014 6:05 AM
  • Hello,

    From a support perspective this is really beyond what we can do here in the forums. If you cannot determine your answer here or on your own, consider opening a support case with us. Visit this link to see the various support options that are available to better meet your needs:  http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    Regards

    Thursday, July 03, 2014 10:25 AM