在Windows服务内部处理启动程序启动过程但应用程序窗口未打开

时间:2014-07-08 13:51:24

标签: c# process windows-services

鉴于以下启动方法:

public static int LaunchExternalApplication(string application, string arguments, string workingDirectory, bool isFireAndForget)
    {
        //Check all required parameters are present
        if (string.IsNullOrEmpty(application))
        {
            throw new Exception("Application Path missing");
        }

        int applicationStatus = -1; //-1 indicates failure
        Process launchApplicationProcess = new Process();

        try
        {
            launchApplicationProcess.StartInfo.FileName = application;
            launchApplicationProcess.StartInfo.Arguments = arguments;

            if (!string.IsNullOrEmpty(workingDirectory))
            {
                launchApplicationProcess.StartInfo.WorkingDirectory = workingDirectory;
            }

            applicationStatus = (launchApplicationProcess.Start() ? 0 : -1);
            Int32 pid = (!launchApplicationProcess.HasExited) ? launchApplicationProcess.Id : 0;

            // only check in on the Process if we care...
            if (!isFireAndForget)
            {
                launchApplicationProcess.WaitForInputIdle(10000); // wait for the window to display
                launchApplicationProcess.WaitForExit(); // wait for the application to do it's thing before exiting
                applicationStatus = launchApplicationProcess.ExitCode; // Get the exitcode for the application to determine if it was successful or not.
            }

        }
        catch (Exception except)
        {
            throw new Exception("An unexpected exception occurred while launching application", except);
        }
        finally
        {
            launchApplicationProcess.Dispose();
        }

        return applicationStatus;
    }

就像一个例子,我将应用程序路径传递给NotePad" C:\ Windows \ system32 \ notepad.exe"我希望它什么时候到达

applicationStatus = (launchApplicationProcess.Start() ? 0 : -1);

记事本会启动,但事实并非如此,启动它的过程但是没有启动应用程序。我不确定这里有什么问题,因为在托管应用程序而不是Windows服务中托管时运行正常。任何人都可以指导我找到正确的方向吗?

0 个答案:

没有答案