AxAcroPDF control windows form hangs when closing

2019-06-04 13:54发布

问题:

I'm dealing with a windows forms .NET application written in C# which needs to display a 3D pdf with all the associated features (rotate the object, change lights and so on). I'm using the AxAcroPDF COM control and it run smoothly. The problem is that when I try to close the form where the control is placed, sometimes it hangs for several seconds with no apparent reason while disposing the control. The 3D file I loaded during the tests is not particulary heavy. The very same file in the standalone adobe reader opens and closes with no particular delay. I'm using the following code within the windows form closing event:

        private void DrawingForm_FormClosing(object sender, FormClosingEventArgs e)
        {
        this.axAcroPDF.Dispose();
        Application.DoEvents();
        CoFreeUnusedLibraries(); }

        [System.Runtime.InteropServices.DllImport("ole32.dll")]
        static extern void CoFreeUnusedLibraries();

The interesting thing is that the form closes normally if the file is just displayed, but, when the object within the 3D file is rotated, it hangs during closing. When it hangs, the icon of a watch (the same shown in standalone Adobe PDF) is displayed. The hanging time is between 15-20 seconds. I tried to display the file within the webbrowser control but I got similar results. What can be the cause of this problem? I tried to search for alternative stable controls but I haven't found any with 3D support.

回答1:

In FormClosing event handler add 2 lines:

this.Controls.Remove(yourAxAcroPDFControl);

yourAxAcroPDFControl = null; Consider it a temporary solution until Adobe has an update. I checked Adobe SDK samples and those cause problems too, despite being written by Adobe themselves so we could expect them to consider it a problem.



回答2:

This is how I fixed it - not great but it works!

protected override void WndProc(ref System.Windows.Forms.Message m)
{
  // WM_CLOSE = 16
  if (16 == m.Msg)
  {
      //closing
      axAcroPDF1.LoadFile("UNLOAD_FILE_FOR_FUDGE");


           // we need to wait a bit
            System.Threading.Thread.Sleep(500);
        }

        base.WndProc(ref m);

 }


回答3:

The solution to the problem is not to call Dispose() but to let the operating system taking care of this instead. I tried and it works perfectly. Why? Maybe .NET calls crossing the COM boundaries cause the operation to slow down?