In my C # application, I got the error message "A disconnected context was detected." what is the cause of the error? How to solve this problem? here i explain my code stream.
I have a stream for collecting data. It collects data from the COM port and saves the data in an EXCEl file.
DATA COLLECTION (THREAD) ---> SCAN 232 PORT (THREAD) ------> PRINT FOR EXECUTION.
I found some reasons for this error from the MSDN library. This suggests MDA help. But I am not an experienced developer in C #. Therefore, I could not understand the problem. Please help me solve this problem.
This is not a complete code for your link. I copied the part that I use to create and write the excel file.
I also have one more problem.
While writing data to excel, if I open any other excel file, the current database file that I use in the code will be opened. how to solve it?
app = new Excel.ApplicationClass(); Workbook1 = app.Workbooks.Add(Type.Missing); Worksheet1 = (Excel.Worksheet)Workbook1.Worksheets[1];//ACtivating sheet-1 of workbook. public bool Load_Excel_file(string Filename) { Excel_Filepath = Filename; try { if (Excel_Filepath != "") { Workbook1.SaveAs(Excel_Filepath, Excel.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } else { lbl_database.Text = "Database OFF"; } if (ExcelFile_Select.Checked == true) { rbtn_database.Checked = true; lbl_database.Text = "Collecting data on Database"; } return (true); } catch { ExcelFile_Select.Checked = false; lbl_database.Text = "Database OFF"; end_excel(); return (false); } } public void Print_to_Excel(object exc_row_cnt_t, object exc_col_cnt_t, object grid_row_cnt_t) { Worksheet1 = (Excel.Worksheet)Workbook1.Worksheets.Add(Type.Missing, (Excel.Worksheet)Workbook1.Worksheets[Sheet_Num++], Type.Missing, Type.Missing); try{ for (int column_count = 1; ((column_count) < Grid_Collect_Data.ColumnCount - UNUSED_CELLS); column_count++) { if ((Grid_Collect_Data.Rows[grid_row_cnt].Cells[column_count].Value) != null) { ((Excel.Range)Worksheet1.Cells[exc_row_cnt, (exc_col_cnt * (Grid_Collect_Data.ColumnCount - UNUSED_CELLS )) + column_count ]).Value2 = (Grid_Collect_Data.Rows[grid_row_cnt].Cells[column_count].Value).ToString(); ((Excel.Range)Worksheet1.Cells[exc_row_cnt, (exc_col_cnt * (Grid_Collect_Data.ColumnCount - UNUSED_CELLS)) + column_count]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; } } Workbook1.Save(); } catch (Exception e) { end_excel(); MessageBox.Show(e.ToString(), "Wireless Sensor Network", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } void end_excel() { Workbook1 = null; Worksheet1 = null; app.Quit(); app = null; if (Worksheet1 != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(Worksheet1); if (Workbook1 != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(Workbook1); if (app != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(app); Workbook1 = null; Worksheet1 = null; app = null; GC.Collect(); Excel_app_run = false; Is_Load_Report = false; }
Error message:
Context 0x1a1178 'is disabled. Release interfaces from the current context (context 0x1a1008). This may result in data corruption or loss. To avoid this problem, make sure that all contexts / apartments remain alive until the application is completely executed with RuntimeCallableWrappers, which represent the COM components that live in them.