Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DisconectedContext was detected

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.

like image 399
chrisszz Avatar asked Nov 15 '25 18:11

chrisszz


1 Answers

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()
like image 62
CodeForever Avatar answered Nov 17 '25 08:11

CodeForever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!