Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disconnected context was detected

Tags:

c#

I got an error message in my c# application "Disconnected context was detected". what is the cause of error? how to solve this problem? here i am explaining my code flow.

I have starts a thread for data collection. It collects the data from com port and save the data in an EXCEl file.

DATA COLLECTION (THREAD) ---> SCAN 232 PORT (THREAD) ------ > PRINT TO EXCEL.

I found some reasons for this error from MSDN library. It says for MDA assistance. But i am not an Experienced developer in c#. So I could not understand the problem . Kindly help me to solve this.

This is not a full code for your reference i copied some portion which one i am using to create and write the excel file.

i am also facing one more problem.

At the time of data writing on the excel, if i will open any other excel file , the current database file which one i am using in the code also will be open. how to solve this?

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 disconnected. Releasing the interfaces from the current context (context 0x1a1008).This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the applicationis completely done with the RuntimeCallableWrappers that represent COM components that liveinside them.

like image 965
Paramasivan Avatar asked Jul 28 '11 08:07

Paramasivan


1 Answers

In my case I'm using Outlook and I got a similar error when trying to access the Outlook Object Model on a thread other than the 'main thread'. In my outlook add-in I also have a Windows form and on the windows form I was starting a new thread that began a long running operation that needed to access the Outlook Object Model. In addition, this thread was trying to update a progress bar on the Windows Form at certain milestones. I made 2 changes that seemed to fix the issue based on the article below:

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

Changes I made:

  • Change the thread to single threaded apartment

  • Remove the code that fires the event to update the progress bar on the main thread

like image 164
public wireless Avatar answered Oct 04 '22 11:10

public wireless