I've an application that automates some file related jobs. Every job is executed inside separate threads. One kind of job is exporting an Excel file to HTML format. I use Microsoft.Office.Interop.Excel
namespace for this purpose. My application was working fine under Windows Server 2008 environment but we upgraded our server to Windows Server 2012 and I started to get the following error :
The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))
The thing is first call to export function successfully exports Excel file to HTML but successive calls fails with the above error. I make sure to close and finalize all my Excel related objects and check from task manager that excel.exe is not working but with no luck.
I use the following code to retry if this error occurs but it keeps getting the exception and fails after 5 retries
while (!success)
{
try
{
ExportExcel();
success = true;
System.Threading.Thread.Sleep(2000);
}
catch (System.Runtime.InteropServices.COMException loE)
{
tryCount++;
if (loE.HResult.ToString("X") == "80010001" || loE.HResult.ToString("X") == "8001010A" && tryCount<5)
{
System.Threading.Thread.Sleep(2000);
}
else
{
throw;
}
}
}
I suspect this might be something related some threading error but I can't come up with an answer. Any insight would be helpful.
Thank you Joe for pointing out the right way:
I ended up using a solution with a mixture of the following links: http://blogs.artinsoft.net/Mrojas/archive/2012/09/28/Office-Interop-and-Call-was-rejected-by-callee.aspx
http://blogs.msdn.com/b/pfxteam/archive/2010/04/07/9990421.aspx
So I used something like the following:
StaTaskScheduler cts=new StaTaskScheduler(1);
TaskFactory factory;
factory = new TaskFactory(cts);
Task jobRunTask = factory.StartNew(() =>
{
MessageFilter.Register();
ExcelInteropFunction();
MessageFilter.Revove();
});