代码之家  ›  专栏  ›  技术社区  ›  Felix Cartwright

.NET(C#)进程和Adobe Acrobat

  •  2
  • Felix Cartwright  · 技术社区  · 16 年前

    诀窍是,当用户做出适当的更改并单击“保存”按钮时,我希望Acrobat窗口关闭.pdf,执行保存,检索“下一个”文件,并打开一个新窗口 立即 打开并显示下一个文件。

        private void OpenViewer()
        {
            // NOTE: pdfView is of type Process, in case you're not familiar with
            // Process.Start().
            pdfView = System.Diagnostics.Process.Start(lnkFile.Links[0].LinkData.ToString());
        }
    
        private bool KillViewer()
        {
            bool result = (pdfView != null);
    
            if (pdfView != null)
            {
                pdfView.CloseMainWindow();
                pdfView.Close();
                pdfView.Dispose();
                pdfView = null;
                GC.Collect();
    
                // Verify that the lock is available before you return, as returning basically says:
                // "Yup, the file's available."
                bool locked = false;
                StreamWriter sw = null;
                do
                {
                    try
                    {
                        sw = new StreamWriter(new FileStream(lnkFile.Links[0].LinkData.ToString(), FileMode.Open));
                        locked = false;
                    }
                    catch (Exception)
                    {
                        locked = true;
                    }
                } while (locked);
    
                sw.Dispose();
            }
    
            return result;
        }
    
        private void SomeButtonEvent
        {
            // Record whether a viewer was open in the first place.
            bool viewerActive = KillViewer(); 
    
            PerformFileLockingMethod();
            GetNextFile()
    
            if(viewerActive)
            {
                OpenViewer();
            }
        }
    

    请注意,在KillViewer()中基本上有一个锁获取循环,以确保程序在pdf查看器完全释放锁之前不会尝试重命名工作文件。

    问题是:有时这一切都运行得很好,有时KillViewer在CloseMainWindow()调用时出现故障,出现InvalidOperationException,details=“进程已退出,因此请求的信息不可用。”。如果没有两件事,这将是相当简单的。。。

    1:pdfView.HasExited=true

    2:

    这到底是怎么可能的?我是否应该使用进程命令来确保窗口关闭?仅供参考,该程序不引用System.*名称空间之外的任何内容,也不引用最终仅引用System.*的内部构建类。

    2 回复  |  直到 16 年前
        1
  •  0
  •   user231691 user231691    16 年前

    试试这个。。

    pdfView.Kill();
    pdfView.WaitForExit();
    
        2
  •  0
  •   Felix Cartwright    16 年前

    经过进一步调查,我想我已经确定发生了什么。

    我没有详细说明工作流程的细节,因为我无法可靠地复制这种情况。经过进一步的尝试,我发现了两种可靠的情况。。。

    1. 单击链接,关闭查看器窗口,然后单击保存。

    在每一种情况下,问题都归结为pdfViewer指出的过程与用户正在做的事情不同步。

    1. 如果单击链接并关闭窗口,pdfViewer变量将保持不变,留下一个HasExited=true的进程。

    为了记录在案,尼克·格雷拉(Nick Guerrera)应该为我指出流程ID而得到分数。这最终解决了问题。