C# Process.Start(“Excel.exe”)立即退出

C# Process.Start(“Excel.exe”)立即退出,c#,system.diagnostics,process.start,C#,System.diagnostics,Process.start,我继承了一些现在不起作用的代码。它应该使用Process类来启动“Excel.exe”。但如果我在下一行中断,我可以看到my process实例出现错误:“process.BasePriority”引发了类型为“System.InvalidOperationException”的异常。下一行代码引发以下异常: System.InvalidOperationException:无法处理请求,因为进程已退出 代码如下: private Microsoft.Office.Interop.Excel.A

我继承了一些现在不起作用的代码。它应该使用Process类来启动“Excel.exe”。但如果我在下一行中断,我可以看到my process实例出现错误:“process.BasePriority”引发了类型为“System.InvalidOperationException”的异常。下一行代码引发以下异常:

System.InvalidOperationException:无法处理请求,因为进程已退出

代码如下:

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;
}
private Microsoft.Office.Interop.Excel.Application StartExcel()
{
//查找已启动Excel应用程序的最大尝试次数
常量int最大尝试数=3;
//在尝试查找已启动的Excel应用程序之间等待的毫秒数
常量int waitTimeMS=200;
Microsoft.Office.Interop.Excel.Application result=null;
//启动Excel
var process=process.Start(“Excel.exe”);
//process.waitForInputId();
//尝试查找已启动的Excel应用程序
int currenttrunt=1;

如果我是你((result==null)&&(currenttrunt),我会尝试用以下内容替换整个方法:

private Microsoft.Office.Interop.Excel.Application StartExcel()
{
    return new Microsoft.Office.Interop.Excel.Application();
}
如果在同一台计算机上安装了多个版本,那么interop可能会使用错误的excel版本

我有两个版本,当
Process.Start(“excel”)
打开较新版本时,我的interop正在打开excel 2013的一个实例


可能这就是所有这些内容的原因?

InvalidOperationException
是由
WaitForInputdle
函数引发的,它等待子进程的消息泵进入并空闲。可能较新版本的Excel.exe只是启动Office365的存根,这不再适用。只是猜测,因此n不是答案,但是你能不能放弃
WaitForInputIdle
调用?@CeeMcSharpface WaitForInputIdle只会延长问题的时间,但进程似乎已经退出。谢谢你的回答。我已经用调用的整个方法更新了问题。你不能从
进程
obje获取你启动的进程吗ct本身?此代码在某种程度上取决于office的实现细节。可能是由于office实例化方式发生了一些细微的变化而导致此代码被破坏。检查process.start之后的流程id是否与任务管理器在桌面上获得实际excel窗口后在PID列中给出的流程id相同。如果不是,则可能不是office安装的excel.exe只是一个存根,您需要使用现有的另一种窗口类名方法。@user1274820正确,流程实例只是一条错误消息“System.InvalidOperationException”。应用程序通过调用简单的Interop.excel.application()运行正常.我想我会坚持接吻,只需去掉这个长长的StartExcel().谢谢