2016-02-05 113 views
1

我已经使用GemBox.Document编写了一个包含接口的c#包装类来完成文档转换。在课堂上,我有以下方法来保存文件:在GemBox.Document.dll中发生未处理的类型'System.StackOverflowException'的异常

public string SourcePath{get;set;} 
public string DestinationType{get;set;} 
public string DestinationPath{get;set;} 
private static DocumentModel document; 

public void ConvertDocument() 
{ 
      try 
      { 
       string filename = Path.GetFileNameWithoutExtension(SourcePath); 
       ComponentInfo.SetLicense(GemboxLicence); 
       string savePath = String.Format("{0}\\{1}.{2}", DestinationPath, filename, DestinationType);    
       document = DocumentModel.Load(SourcePath); 
       document.Save(savePath); 

      } 
      catch (Exception e) 
      {    
       throw (new Exception("An error occured while saving the document: " + e.Message)); 
      } 

} 

类工作得很好,当我把它从另一个C#程序。

我注册的类DLL对COM并创建了一个TLB文件与regasm如下:

regasm MBD.GemBox.Document.dll /tlb 

我想通过德尔福COM访问的dll,所以我进口TLB文件到德尔福2009年我然后创建调用C#DLL的包装德尔福库:

procedure ConvertDocument(sourcePath : string ; destinationType : string ; destinationPath : string); 
var 
    doc : TDocumentConvertor; 
begin 
    try 
     OleInitialize(nil); 
     doc := TDocumentConvertor.Create(nil); 
     doc.Connect; 
     doc.SourcePath := sourcePath ; 
     doc.DestinationType := destinationType; 
     doc.DestinationPath := destinationPath; 
     doc.ConvertDocument; 
     doc.Disconnect; 
     doc.Free; 
     CoUninitialize; 
    except 
    on E:Exception do 
    begin 
     Writeln(E.Classname, ': ', E.Message); 
    end; 
    end; 
end; 

不过,我得到了

“未处理在GemBox.Document.dll中发生类型'System.StackOverflowException'异常“

当我尝试通过delphi调用该方法时。有谁知道为什么发生这种情况?

+2

您需要调试您的代码。看来你有一个堆栈溢出错误。 –

+0

如果您只是将WordDocuments转换为另一种类型,您可能会喜欢此https://github.com/tobya/DocTo –

回答

1

我由Gembox文档转换部分移动到C#代码一个单独的线程解决了这个问题:

string filename = Path.GetFileNameWithoutExtension(SourcePath); 
       ComponentInfo.SetLicense(GemboxLicence); 
       string savePath = String.Format("{0}\\{1}.{2}", DestinationPath, filename, DestinationType);    
       document = DocumentModel.Load(SourcePath); 
       document.Save(savePath); 

解决方案,我创建了一个单独的类,它产生一个独立的线程来处理文档转换:

using System; 
using System.Collections; 
using System.Collections.Concurrent; 
using System.Threading; 
using GemBox.Document; 
using System.Runtime.InteropServices; 

namespace GB.Document 
{ 
    public class GbWorkItem 
    { 
     public string Source; 
     public string Destination; 
    } 

    internal class GbThreadData 
    { 
     public bool Running; 
     public Queue WorkQueue = Queue.Synchronized(new Queue()); 
    } 

    public class GemBox : IDisposable 
    { 
     private static GbThreadData _data = new GbThreadData(); 
     private bool _disposed = false; 

     public GemBox() 
     { 
      Thread thread = new Thread(ThreadExec); 
      thread.Start(_data); 
     } 

     internal static void ThreadExec(object o) 
     { 
      _data = o as GbThreadData; 
      _data.Running = true; 

      while (_data.Running) 
      { 
       if (_data.WorkQueue.Count == 0) Thread.Sleep(250); 
       else 
       { 
        lock (_data.WorkQueue.SyncRoot) 
        { 
         try 
         { 
          GbWorkItem work = _data.WorkQueue.Dequeue() as GbWorkItem; 
          DocumentModel document = DocumentModel.Load(work.Source); 
          document.Save(work.Destination); 
         } 
         catch (InvalidOperationException) { } 
        } 
       } 
      } 
     } 

     public void QueueDocument(GbWorkItem item) 
     { 
      lock (_data) 
      { 
       _data.WorkQueue.Enqueue(item); 
      } 
     } 
     public void Dispose() 
     { 
      Dispose(true); 
      GC.SuppressFinalize(this); 
     } 
     protected virtual void Dispose(bool disposing) 
     { 
      if (_disposed) 
       return; 

      if (disposing) 
      { 
       _data.Running = false; 
      } 
      _disposed = true; 
     } 
    } 
} 

,然后我做了如下修改初始类:

public string SourcePath{get;set;} 
     public string DestinationType{get;set;} 
     public string DestinationPath{get;set;} 
     private static DocumentModel document; 
     private GemBox _gemBox; 

     public DocumentConvertor() 
     {  
     Initialise(); 
     } 

     public void ConvertDocument() 
     { 
        try 
        { 
         // string filename = Path.GetFileNameWithoutExtension(SourcePath); 
         //ComponentInfo.SetLicense(GemboxLicence); 
         string savePath = String.Format("{0}\\{1}.{2}", DestinationPath, filename, DestinationType);    
         // document = DocumentModel.Load(SourcePath); 
         // document.Save(savePath); 
         _gemBox.QueueDocument(new GbWorkItem { Source = SourcePath, Destination = savePath });     

        } 
        catch (Exception e) 
        {    
         throw (new Exception("An error occured while saving the document: " + e.Message)); 
        } 
        finally 
        { 
         Destroy(); 
        } 

     } 

     private void Initialise() 
      {   
      _gemBox = new GemBox(); 
      } 

     private void Destroy() 
      {  
      _gemBox.Dispose(); 
      } 

Delphi代码保持不变。

相关问题