Given the following launch method:
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;
}
Just as an example I pass in the application path to NotePad "C:\Windows\system32\notepad.exe" I would expect when it hits the line
applicationStatus = (launchApplicationProcess.Start() ? 0 : -1);
Notepad would get launched, but this is not the case, the process for it is started but the application is not launched. I'm unsure what is at fault here as this will run fine when hosted in a console app and not a windows service. Can anyone guide me in the right direction on what to look for?