Disconnected context was detected
<p>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.</p>

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

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

<p>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.</p>

<p>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.</p>

<p>i am also facing one more problem.</p>

<h2>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?</h2>

<pre><code>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) &lt; 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;


    }
</code></pre>

<p>Error Message :</p>

<p>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.</p>
