Visual C# Developer Center > Visual C# Forums > Visual C# General > ContextSwitchDeadlock was detected
Ask a questionAsk a question
 

AnswerContextSwitchDeadlock was detected

  • Tuesday, November 03, 2009 9:13 PMJon Q Jacobs Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    What can I do not to get this error? I can click continue, but I want this to run without intervention.
    Here is distilled code where this happens:

    OleDbConnection con = new OleDbConnection(cons);

    OleDbCommand acmd = con.CreateCommand();

    con.Open();

    foreach (string tab in Tabs)
    {

      TextFileReadsAndStringManipulation();

      acmd.CommandText = qry;
      OleDbDataReader rdr = acmd.ExecuteReader(); 

      while (rdr.Read())
      {
        for (int i = 0; i < flds.Length; i++)
        {
          string s = rdr.GetValue(i).ToString().Replace("&", "&amp;");
          s = s.Replace("<", "&lt;");
          flds[i] = s.Replace(">", "&gt;");
        }
        string arow = String.Format(row, flds);
        wks = wks.Replace(Here, arow);
      }
      rdr.Close();
    }

Answers

  • Wednesday, November 04, 2009 6:03 PMJon Q Jacobs Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Thanks,

    I found a similar question posted for vb, so
    I added Application.DoEvents() to be called about every 20th pass through the loop, and that took care of it.

    Jon

All Replies

  • Tuesday, November 03, 2009 9:27 PMScottyDoesKnow Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It means your interface is busy for over a minute or something like that. What you should do is put your long calculations into a separate thread, like a backgroundworker. You could also just turn off the error:

    Debug -> Exceptions -> somewhere in there, i don't remember where
  • Wednesday, November 04, 2009 6:03 PMJon Q Jacobs Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Thanks,

    I found a similar question posted for vb, so
    I added Application.DoEvents() to be called about every 20th pass through the loop, and that took care of it.

    Jon