none
Getting deadlock when reading the file RRS feed

  • Question

  • Hi,
    I get
    ContextSwitchDeadlock occurred
    Message: Managed Debugging Assistant 'ContextSwitchDeadlock' has detected a problem in 'C:\App\WindowsFormsApplication9_1\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe'.
    Additional information: The CLR has been unable to transition from COM context 0x7015b0 to COM context 0x701720 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.



    using these
                        var ExcelObj = new Excel.Application();
                        //Excel.Workbook Book0 = ExcelObj.Workbooks.Open(openFileDialog1.FileName, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);
                        ExcelObj.ScreenUpdating = false;
    
                        Excel.Worksheet Worksheet0;
    
                        using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fs, false))
                            {
                                WorkbookPart workbookPart = doc.WorkbookPart;
    
                                Sheet theSheet = workbookPart.Workbook.Descendants<Sheet>().
                                    Where(s => s.Name == "Sheet Overall").FirstOrDefault();
    
                                if (theSheet == null)
                                {
                                    throw new ArgumentException("sheetName");
                                }
    
                                int sheetIndex = 0;
                                foreach (WorksheetPart wsPart in workbookPart.WorksheetParts)
                                {
                                    WorkbookStylesPart wstylePart = workbookPart.WorkbookStylesPart;
                                    Stylesheet ss = wstylePart.Stylesheet;
    
                                    string sheetName = workbookPart.Workbook.Descendants<Sheet>().ElementAt(sheetIndex).Name;
                                    if (sheetName != "Sheet Overall")
                                    {
                                        if (sheetName == null)
                                        {
                                            throw new ArgumentException("sheetName");
                                        }
    
                                        using (StreamWriter sw = new StreamWriter(@File1, true, Encoding.Unicode))
                                        {
                                            sw.WriteLine("Sheet name: {0}", sheetName);
                                        }
    
                                        var cells = wsPart.Worksheet.Descendants<Cell>();
    
                                        foreach (Cell cell in cells)
                                        {
                                            int i;
                                            //if (string.IsNullOrEmpty(cell.StyleIndex.Value.ToString()))
                                            if (cell.StyleIndex != null)
                                            {
                                                i = Convert.ToInt32(cell.StyleIndex.Value);
    
                                                //Column cellColumn = wsPart.Worksheet.Descendants<Column>().ElementAt(GetColumnIndex(cell.CellReference.Value));
    
                                                DocumentFormat.OpenXml.Spreadsheet.CellFormat cellFormat = ss.Descendants<DocumentFormat.OpenXml.Spreadsheet.CellFormat>().ElementAt(i);
    
                                                int j = Convert.ToInt32(cellFormat.FontId.Value);
                                                int k = Convert.ToInt32(GetColumnIndex(cell.CellReference.Value));
                                                DocumentFormat.OpenXml.Spreadsheet.Font font = ss.Descendants<DocumentFormat.OpenXml.Spreadsheet.Font>().ElementAt(j);
                                                FontSize fontsize = font.FontSize;  
    
                                                //using (StreamWriter sw = new StreamWriter("D:\\cellValue.txt", true, Encoding.Unicode))
                                                foreach (Column cellColumn in wsPart.Worksheet.Descendants<Column>())
                                                {
                                                    if (cellColumn.Min.Value == j)
                                                    {
                                                        var cellWidth = cellColumn.Width.Value;
                                                        using (StreamWriter sw = new StreamWriter(@File1, true, Encoding.Unicode))
                                                        {
                                                            sw.WriteLine("Cell contents: {0}", GetCellValue(cell, workbookPart));
                                                            sw.WriteLine("Cell width: {0}", cellWidth.ToString());
                                                            sw.WriteLine("Cell font: {0}", fontsize.Val.ToString());
                                                        }
                                                    }
    
                                                }   
                                            }
                                        }
    
                                    }
                                    sheetIndex++;
                                }
                            }
                        }



    Many Thanks & Best Regards, Hua Min


    Tuesday, August 16, 2016 2:20 AM

Answers

  • Hi HuaMin,

    After checking ContextSwitchDeadlock, it is a warning from a Managed Debugging Assistant, and it doesn’t necessarily mean your code has an issue. You could go to Debug->Exceptions->Managed Debugging Assistants->disable  ContextSwitchDeadlock to disable this message.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Jackson_1990 Thursday, August 18, 2016 10:07 AM
    Thursday, August 18, 2016 8:49 AM

All replies

  • Hi HuaMin,

    Could you share us why you mix up using Excel automation and Open XML SDK in the same project?

    Which line you got this error? It would be helpful if you could share us a simple demo which could reproduce your issue.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, August 17, 2016 1:48 AM
  • Hi Sir,

    I'm to process relevant Excel file by the above codes, using OpenXML and have already got the deadlock. Let me know if you cannot get the sample Excel file as attached. Thanks a lot.


    Many Thanks & Best Regards, Hua Min




    Wednesday, August 17, 2016 2:56 AM
  • Any advice?

    Many Thanks & Best Regards, Hua Min

    Thursday, August 18, 2016 4:48 AM
  • Hi HuaMin,

    After checking ContextSwitchDeadlock, it is a warning from a Managed Debugging Assistant, and it doesn’t necessarily mean your code has an issue. You could go to Debug->Exceptions->Managed Debugging Assistants->disable  ContextSwitchDeadlock to disable this message.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Jackson_1990 Thursday, August 18, 2016 10:07 AM
    Thursday, August 18, 2016 8:49 AM