DisconectedContext was detected

3

I need help getting rid of this error:

"Transition into COM context 0x465608 for this RuntimeCallableWrapper failed with the following error: System call failed. (Exception from HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED)). This is typically because the COM context 0x465608 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else. Releasing the interfaces from the current COM context (COM context 0x465498). This may cause corruption or data loss. To avoid this problem, please ensure that all COM contexts/apartments/threads stay alive and are available for context transition, until the application is completely done with the RuntimeCallableWrappers that represents COM components that live inside them."

Which occurs during the execution of this code:

int i = 2;

while(i <= lastRowPOPT)
{
     RefDocNo = poptSheet.Cells[i, 1].Text;
     RefItem = poptSheet.Cells[i, 2].Text;
     Plnt = poptSheet.Cells[i, 3].Text;
     concat = RefDocNo + RefItem + Plnt;
     poptSheet.Cells[i, 8] = concat;
     poptSheet.Range["E" + i, "G" + i].Copy(Type.Missing);
     poptSheet.Range["I" + i, "K" + i].PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteAll);

     i++;
}

There are around 4700 rows and it does it's job on 1000 and something, then it throws that. Also the execution lasts about 4 mins, but I think it stalls most of that time.

c#
winforms
excel-interop
asked on Stack Overflow Mar 24, 2014 by chrisszz • edited Mar 24, 2014 by Microsoft DN

2 Answers

2

I've figured it out. Apparently, when the outside process (in this case, Excel) runs for too long, the OS thinks it has taken over and not letting other applications run. The diagnosis was dreadful, but the solution is very simple. Add the following line of code in one or more places to allow other processes to execute, and now the long execution completes without issue.

System.Windows.Forms.Application.DoEvents()
answered on Stack Overflow Sep 22, 2016 by CodeForever • edited Sep 22, 2016 by Petter Friberg
0

Since I didn't find any resolution for that, I used an OleDbConnection and a DataSet instead of Interop services so everything works fine like that.

answered on Stack Overflow Mar 27, 2014 by chrisszz

User contributions licensed under CC BY-SA 3.0