2009-11-08 38 views
4

我有一个小应用程序,可以在OOo Writer中生成三个不同的模板文档。当点击的三个“生成”按钮之一,这是所执行的代码的一部分(在C#):在OpenOffice中,如何检查我的XComponentContext是否已关闭?

// Connect to OOo 
if (componentContext == null) 
    componentContext = uno.util.Bootstrap.bootstrap(); 
XMultiServiceFactory multiServiceFactory = 
    (XMultiServiceFactory) componentContext.getServiceManager(); 
XComponentLoader loader = (XComponentLoader) 
    multiServiceFactory.createInstance 
     ("com.sun.star.frame.Desktop"); 

// Initialize class members document, text, and cursor 
document = (XTextDocument) loader.loadComponentFromURL 
    ("private:factory/swriter", "_blank", 0, 
    new PropertyValue[0]); 
text = document.getText(); 
cursor = text.createTextCursor(); 

以下步骤导致崩溃:

  1. 用户生成文件。
  2. 用户关闭文档(关闭OOo)。
  3. 用户尝试生成另一个文档。

时抛出此异常:

unoidl.com.sun.star.lang.DisposedException: URP-Bridge: disposed(tid=4) Unexpected connection closure 

如何检查,以确保连接仍试图生成另一个图表前,开放?如果它已关闭,我该如何重新连接?

编辑:更具体地说,这是完全错误消息:

Marshaling clicked signal 
Exception in Gtk# callback delegate 
    Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception. 
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> unoidl.com.sun.star.lang.DisposedException: URP-Bridge: disposed(tid=4) Unexpected connection closure 
    at com.sun.star.bridges.mono_uno.UnoInterfaceProxy.ConstructReturnMessage (Any result, System.Object[] args, uno.Typelib.InterfaceMethodTypeDescription* methodTD, IMethodCallMessage callmsg, Any exception) [0x00000] 
    at com.sun.star.bridges.mono_uno.UnoInterfaceProxy.Invoke (IMessage request) [0x00000] 
    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke (System.Runtime.Remoting.Proxies.RealProxy rp, IMessage msg, System.Exception& exc, System.Object[]& out_args) [0x00000] 
    --- End of inner exception stack trace --- 
    at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] 
    at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] 
    at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x00000] 
    at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00000] 
    at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] 
    at GLib.Signal.ClosureInvokedCB (System.Object o, GLib.ClosureInvokedArgs args) [0x00000] 
    at GLib.SignalClosure.Invoke (GLib.ClosureInvokedArgs args) [0x00000] 
    at GLib.SignalClosure.MarshalCallback (IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data) [0x00000] 
    at GLib.ExceptionManager.RaiseUnhandledException(System.Exception e, Boolean is_terminal) 
    at GLib.SignalClosure.MarshalCallback(IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data) 
    at Gtk.Application.gtk_main() 
    at Gtk.Application.Run() 
    at TestDrive.MainClass.Main(System.String[] args) in /home/matthew/Dropbox/OpenSBS-mono/TestDrive/Main.cs:line 28 

The application was terminated by a signal: SIGHUP 

如果我摆脱线if (componentContext == null)(即,总是尝试连接,即使我们已经连接了),我得到一个栈跟踪伴随此消息:

================================================================= 
Got a SIGSEGV while executing native code. This usually indicates 
a fatal error in the mono runtime or one of the native libraries 
used by your application. 
================================================================= 
+0

我有这个相同的问题。你有没有设法找出一个解决方案或解决方法? – 2013-03-14 11:08:27

+0

@Doomy:我的记忆有点模糊,但我认为这个(丑陋的)解决方法奏效:我创建了一个单独的可执行文件,用于启动OOo并根据需要与其交互。每次我需要与OOo进行交互时,我都会从主程序调用这个助手可执行文件(将它传递给它需要的任何参数)。因为我只在一次调用助手可执行文件的时候打开过OOo,所以我可以避免这个错误。 – 2013-03-14 18:26:23

+0

感谢您的回应。我的解决方法是安装OpenOffice Quickstarter功能并在运行我的应用程序之前确保它正在运行。 – 2013-03-15 14:07:02

回答

2

这只是一个猜测。您可以使用XComponent.addEventListener并监听disposing事件。

如:

class App : XEventListener 
{ 
    private XComponentLoader m_loader; 

    private XComponentLoader Loader 
    { 
     get 
     { 
      if (m_loader == null) 
      { 
       m_loader = (XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop"); 
       XComponent comp = (XComponent)m_loader; 
       comp.addEventListener(this); 
      } 
      return m_loader; 
     } 
    } 

    private void disposing(EventObject Source) 
    { 
     m_loader = null; 
    } 
} 
+0

这似乎并不奏效。在获取multiServiceFactory时,我得到我的错误,而不是在获取加载器时,但更改代码以调整此代码似乎没有帮助。 一个有趣的事情是,当我关闭OOo时,“配置”似乎没有被调用componentContext,multiServiceFactory或loader。只有文档调用处理(并尝试重新连接仍然给我错误)。 也许发生的事情是,连接永远不会完全关闭,这就是引导程序给出错误的原因。有任何想法吗? – 2009-11-21 17:49:03

2

我发现,这个错误不会发生,如果OpenOffice的快速启动应用程序运行(位于C:\Program Files (x86)\OpenOffice.org 3\program\quickstart.exe)。 quickstart.exe似乎要求soffice.exe,并且即使在用户关闭最后一个文档窗口后,它仍将保持运行状态。

如果quickstart.exe丢失,可以通过OpenOffice安装程序进行安装。您的应用程序可以通过使用System.Diagnostics.Process.Start()启动quickstart.exe来确保soffice.exe正在运行。如果它已经在运行,它将不会复制该进程。