locked
System.Diagnostics.Process and Acrobat Reader RRS feed

  • Question

  • If I already have any PDF file being displayed in Adobe Reader, making the call System.Diagnostics.Process proc = Launch(sUniqueName, sPath, true, m_iWaitNone), then immediately examining proc.HasExited would indicate true, although the "sUniqueName" PDF would still be open.  This can't be correct!?

    If I don't have any PDF files open before making the call, "sUniqueName" would be open, and examining proc.HasExited would indicate false... which is correct.  This is very repeatable.  Why would proc.HasExited = true when the PDF is still being displayed just because another PDF was opened prior to the call?

    Thanks for any insight... this is driving me crazy!   :-P

    -Don

    -------------

    Code follows:

          #region Launch()
            public System.Diagnostics.Process Launch(string sFilename, string sArgs, bool bCreateWindow, int iWaitMode)
            {
                const int ERROR_FILE_NOT_FOUND = 2;
                const int ERROR_ACCESS_DENIED = 5;

                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(sFilename);
                proc.StartInfo.WorkingDirectory = fldFILEINFO.sStartFolder;
                proc.EnableRaisingEvents = true;
                proc.StartInfo.UseShellExecute = true;
                proc.StartInfo.Arguments = sArgs;
                if (!bCreateWindow)
                {
                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
                }
                else
                {
                    proc.StartInfo.CreateNoWindow = false;
                    proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                }
              
                proc.StartInfo.FileName = sFilename;
     
                try
                {
                    proc.Start();
                    switch (iWaitMode)
                    {
                        case m_iWaitNone:
                            break;
                        case m_iWaitTimeout:
                             proc.WaitForExit(m_iRetrieveWaitSeconds * 1000);
                            break;
                        case m_iWaitIndefinately:
                             proc.WaitForExit();
                            break;
                    }
                    return proc;
                }

                catch (Win32Exception e)
                {
                    if (e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
                        MessageBox.Show("File not found - " + sFilename + ".", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
                    {
                        MessageBox.Show(e.Message + ". You do not have permission to run " + sFilename + ".", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                return null;
            }

    Tuesday, April 5, 2011 11:00 PM

Answers

  • Hi Don again,

     

    I provided the code in last post to detect what the process is. If you add the code to your project, you will find that the process is used to open the PDF file but not mean that it is the PDF process.

    After the PDF file is opend, the process will be over so that HasExited is true then.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Thursday, April 14, 2011 5:17 AM
    Monday, April 11, 2011 2:44 AM

All replies

  • Hi Don,

     

    Welcome to the MSDN forum!

     

    I think you can add code to check the process after starting it and getting retrieve value from Launch method:

    MessageBox.Show("ProcessID: " + proc.Id.ToString() + "\r\n" + "ProcessName: " + proc.ProcessName + "\r\n" + "ProcessHasExited: " + proc.HasExited.ToString());

     

    A value of true for HasExited indicates that the associated process has terminated, either normally or abnormally.

    More information about Process.HasExited Property

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 6, 2011 6:17 AM
  • That's what I thought... but it is not working that way for me.  If any PDF was opened prior to the call, the proc.HasExited will equal true even though the launch of the second PDF was successful.  If a PDF was not already opened prior to the call, the proc.HasExited returns false as it should.  This is very consistent for me. 

    Thanks for the help with this.  I really appreciate it.

    Don

     

     

    Saturday, April 9, 2011 3:34 PM
  • Hi Don again,

     

    I provided the code in last post to detect what the process is. If you add the code to your project, you will find that the process is used to open the PDF file but not mean that it is the PDF process.

    After the PDF file is opend, the process will be over so that HasExited is true then.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Thursday, April 14, 2011 5:17 AM
    Monday, April 11, 2011 2:44 AM