在单独的AppDomain中运行PDFTron:“无法通过AppDomains传递GCHandle”

时间:2017-10-19 09:14:44

标签: c# appdomain pdftron

PDFTron似乎无法处理多个AppDomain。从单独的AppDomain运行PDFTron时,应用程序崩溃时出现未处理的异常(“无法通过AppDomains传递GCHandle”)。

   bei System.Runtime.InteropServices.GCHandle.InternalCheckDomain(IntPtr handle)
   bei System.Runtime.InteropServices.GCHandle.FromIntPtr(IntPtr value)
   bei gcroot<pdftron::PDF::PDFViewWPF ^>.->(gcroot<pdftron::PDF::PDFViewWPF ^>* )
   bei pdftron.PDF.detail.DeluxeCreateTile(Void* data, SByte* buffer, UInt32 width, UInt32 height, UInt32 stride, UInt32 page_num, UInt64 x_page_loc, UInt64 y_page_loc, UInt32 zoomed_page_width, UInt32 zoomed_page_height, UInt32 tile_id, UInt32 x_in_page, UInt32 y_in_page, Int32 canvas_id, Int32 remaining_tiles, Int32 tile_type, Int32 sequenceNum)

在搜索解决方案时,我在2011年创建的Google网上论坛中发现了一个帖子。用户在他正在处理的Outlook插件中遇到了同样的异常。根据该主题,PDFTron支持已知该问题,并且还在基于WinForms的控件之一中修复了该问题。您可以在此处找到讨论:https://groups.google.com/forum/#!topic/pdfnet-sdk/DGki3Fj2_FU

我是否必须考虑某些事情,或者PDFTron根本无法在此环境中运行?

这是我用来重现问题的示例中的代码:

MainWindow.xaml

<Window x:Class="RunningPdfTronUsingMultipleAppDomains.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:RunningPdfTronUsingMultipleAppDomains"
        xmlns:pdf="clr-namespace:pdftron.PDF;assembly=PDFNet"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="LayoutRoot">
        <pdf:PDFViewWPF x:Name="PDFView"/>
        <Button Panel.ZIndex="100" Height="20" Width="110" Content="Create AppDomain" PreviewMouseLeftButtonDown="Button_PreviewMouseLeftButtonDown"/>
    </Grid>
</Window>

MainWindow.xaml.cs

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
    {
        PDFDoc document = new PDFDoc("SampleDocument.pdf");
        PDFView.SetDoc(document);
    }

        private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            AppDomain appDomain = AppDomain.CreateDomain(Guid.NewGuid().ToString());
            var assembly = System.Reflection.Assembly.GetAssembly(typeof(MainWindow));
            appDomain.Load(assembly.GetName());
            Proxy proxy = (Proxy)appDomain.CreateInstanceAndUnwrap(System.Reflection.Assembly.GetExecutingAssembly().GetName().FullName, typeof(Proxy).FullName);
            Thread thread = new Thread(new ParameterizedThreadStart((o) => proxy.Invoke())) { ApartmentState = ApartmentState.STA };
            thread.Start();
        }
    }

Proxy.cs

  public class Proxy : MarshalByRefObject
        {
            public void Invoke()
            {
                try
                {
                    App app = new App();
                    app.Run(new MainWindow());
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

仅供参考:PDFTron WPF 6.8(定于1月份)将包括对多个/单独AppDomains的支持