Disabled context detected

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.

+6
source share
3 answers

In my case, I use Outlook, and I get a similar error when trying to access the Outlook object model in a thread other than the "main thread". In my add-in add-on, I also have a Windows form, and in the Windows form I started a new thread that started the lengthy operation needed to access the Outlook object model. In addition, this thread tried to update the progress bar in Windows Form at certain stages. I made 2 changes which seemed to fix the problem based on the following article:

http://msdn.microsoft.com/en-us/library/8sesy69e%28v=vs.100%29.aspx#feedback

The changes I made:

  • Change thread to single-threaded apartment

  • Remove the code that triggers the event to update the progress bar of the main thread

+1
source

If I understand your question, is the problem that Excel shows your file when opened? It is expected that all your actions with your code will be handled by Excel. Thus, Excel will show your file, it does not "know" it is not a user file.

If you really do not want this, then not using COM / Interop is the way forward. Browse through the OpenXML library and the ClosedXML library, making it easy to work with the previous and Excel.

Finally, if you are really trying to write every minute ... Excel is not the format you want to write. Look at plain text, XML, or perhaps an embedded database.

0
source

You can remove the exception by disabling the exception:

  • Select a project.
  • Go to menu: Debugging-> Exceptions
  • Go to managed debugging exception. And remove the check from the type of exception you experienced.
-4
source

Source: https://habr.com/ru/post/893752/


All Articles