问题
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.
回答1:
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()
回答2:
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.
来源:https://stackoverflow.com/questions/22606666/disconectedcontext-was-detected