I inherited some code that is now not working. It is supposed to use the Process class to start "Excel.exe". But if I break on the next line I can see the my process instance has the error: 'process.BasePriority' threw an exception of type 'System.InvalidOperationException'. The next line of code throws the following exception:
System.InvalidOperationException: Cannot process request because the process has exited.
Here is the code:
private Microsoft.Office.Interop.Excel.Application StartExcel()
{
// Maximum number of attempts to look for started Excel Application
const int maxAttempts = 3;
// Number of milliseconds to wait between attempts to look for started Excel Application
const int waitTimeMS = 200;
Microsoft.Office.Interop.Excel.Application result = null;
// Start Excel
var process = Process.Start("Excel.exe");
//process.WaitForInputIdle();
// Try to find started Excel Application
int currentAttempt = 1;
while ((result == null) && (currentAttempt <= maxAttempts))
{
// Wait between attempts
if (currentAttempt != 1)
{
Thread.Sleep(waitTimeMS);
}
// List all running Excel automation objects and find the one with the same process id
IRunningObjectTable lRunningObjectTable = null;
IEnumMoniker lMonikerList = null;
try
{
// Query Running Object Table
if (GetRunningObjectTable(0, out lRunningObjectTable) == 0 && lRunningObjectTable != null)
{
// List Monikers
lRunningObjectTable.EnumRunning(out lMonikerList);
// Start Enumeration
lMonikerList.Reset();
// Array used for enumerating Monikers
IMoniker[] lMonikerContainer = new IMoniker[1];
IntPtr lPointerFetchedMonikers = IntPtr.Zero;
// foreach Moniker
while (lMonikerList.Next(1, lMonikerContainer, lPointerFetchedMonikers) == 0)
{
object lComObject;
lRunningObjectTable.GetObject(lMonikerContainer[0], out lComObject);
// Check the object is an Excel workbook
if (lComObject is Microsoft.Office.Interop.Excel.Workbook)
{
Microsoft.Office.Interop.Excel.Workbook lExcelWorkbook = (Microsoft.Office.Interop.Excel.Workbook)lComObject;
// Get the Process ID for the Window Handle
uint processId;
GetWindowThreadProcessId(new IntPtr(lExcelWorkbook.Application.Hwnd), out processId);
if (processId == process.Id)
{
// Correct automation object found, return Application
result = lExcelWorkbook.Application;
break;
}
}
}
}
}
finally
{
// Release ressources
if (lRunningObjectTable != null) Marshal.ReleaseComObject(lRunningObjectTable);
if (lMonikerList != null) Marshal.ReleaseComObject(lMonikerList);
}
currentAttempt++;
}
return result;
}
At the end result = null. Also an excel workbook does appear on my screen but for some reason the process cannot find the Excel Workbooks process ID. I am guessing this is because the process has excited without closing Excel.exe.
It has been running fine for the past year, and just stopped working a couple of days ago. I do not have any experience with the Process class and am not sure what the issue might be as the errors seem very vague. Based on other SO threads it seems it may be exiting. I can not figure out why the process is immediately Exiting.
Edited for clarification.