OpenOffice.org 关闭后如何重新连接?

发布于 2024-08-12 08:44:50 字数 2135 浏览 3 评论 0原文

这是一个示例应用程序,它创建一个内部带有单个按钮的窗口。单击时,它会连接到 OOo(如果尚未连接)并创建一个文本文档。

除非所有在 OOo 中创建的文档都被关闭,否则这工作正常。然后,当我尝试创建下一个图表时,我收到了 DisposeException。这是可以理解的,但是OOo已经关闭了。但是,此时尝试重新连接会出现段错误。有更好的重新连接方法吗?我正在Linux(Ubuntu)上工作。

注意:即使 OOo 未打开,也可以正确连接到 OOo。一旦 OOo 被应用程序打开,然后关闭,我们就会收到错误。

您真正需要查看的是 Connect 方法。我只是将它包装在 Gtk 接口中以便于测试。

using System; 
using unoidl.com.sun.star.uno; 
using unoidl.com.sun.star.lang; 
using unoidl.com.sun.star.text; 
using unoidl.com.sun.star.frame; 
using unoidl.com.sun.star.beans; 
using Gtk; 

namespace TestOOo { 
   class MainClass { 
      static XComponentContext componentContext; 
      static XMultiServiceFactory multiServiceFactory; 
      static XComponentLoader loader; 
      static XTextDocument document; 

      public static void Main (string[] args) 
      { 
         Application.Init(); 

         Window mainWindow = new Window("Test Window"); 
         mainWindow.Visible = true; 
         mainWindow.Destroyed += delegate { Application.Quit(); }; 
         Button button = new Button(Stock.Ok); 
         button.Clicked += delegate { Connect(); }; 
         mainWindow.Add(button); 
         mainWindow.ShowAll(); 

         Application.Run(); 
      } 

      static void Connect() 
      { 
         // Connect to OOo 
         if (componentContext == null) 
            componentContext = uno.util.Bootstrap.bootstrap(); 

         try { 
            multiServiceFactory = 
               (XMultiServiceFactory) componentContext.getServiceManager(); 
         } catch { 
            // This is where we want to reconnect, but trying to 
            // bootstrap() again segfaults. 

            // componentContext = uno.util.Bootstrap.bootstrap(); 
            // multiServiceFactory = 
            //   (XMultiServiceFactory) componentContext.getServiceManager(); 
         } 

         loader = (XComponentLoader) 
            multiServiceFactory.createInstance("com.sun.star.frame.Desktop"); 
         document = (XTextDocument) loader.loadComponentFromURL 
            ("private:factory/swriter", "_blank", 0, new PropertyValue[0]); 
      } 
   } 
}

Here is a sample application that creates a Window with a single Button inside. When clicked, it connects to OOo (if not already connected) and creates a text document.

This works fine unless all the documents created in OOo are closed. Then, I get a DisposedException when trying to create the next chart. This is understandable, but OOo has been closed. However, trying to reconnect at this point gives me a segfault. Is there a better way to reconnect? I am working on linux (Ubuntu).

Note: This connects properly to OOo even if OOo is not open. It's once OOo has been opened by the application, then closed that we get the error.

All you really need to look at is the Connect method. I just wrapped it in a Gtk interface for easy testing.

using System; 
using unoidl.com.sun.star.uno; 
using unoidl.com.sun.star.lang; 
using unoidl.com.sun.star.text; 
using unoidl.com.sun.star.frame; 
using unoidl.com.sun.star.beans; 
using Gtk; 

namespace TestOOo { 
   class MainClass { 
      static XComponentContext componentContext; 
      static XMultiServiceFactory multiServiceFactory; 
      static XComponentLoader loader; 
      static XTextDocument document; 

      public static void Main (string[] args) 
      { 
         Application.Init(); 

         Window mainWindow = new Window("Test Window"); 
         mainWindow.Visible = true; 
         mainWindow.Destroyed += delegate { Application.Quit(); }; 
         Button button = new Button(Stock.Ok); 
         button.Clicked += delegate { Connect(); }; 
         mainWindow.Add(button); 
         mainWindow.ShowAll(); 

         Application.Run(); 
      } 

      static void Connect() 
      { 
         // Connect to OOo 
         if (componentContext == null) 
            componentContext = uno.util.Bootstrap.bootstrap(); 

         try { 
            multiServiceFactory = 
               (XMultiServiceFactory) componentContext.getServiceManager(); 
         } catch { 
            // This is where we want to reconnect, but trying to 
            // bootstrap() again segfaults. 

            // componentContext = uno.util.Bootstrap.bootstrap(); 
            // multiServiceFactory = 
            //   (XMultiServiceFactory) componentContext.getServiceManager(); 
         } 

         loader = (XComponentLoader) 
            multiServiceFactory.createInstance("com.sun.star.frame.Desktop"); 
         document = (XTextDocument) loader.loadComponentFromURL 
            ("private:factory/swriter", "_blank", 0, new PropertyValue[0]); 
      } 
   } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴者 2024-08-19 08:44:50

我一直无法弄清楚这一点,但我确实找到了一种方法来解决它:

我创建了一个单独的可执行文件来进行生成。然后,我的主应用程序调用此可执行文件,并向其传递必要的参数(只是要生成的文件的路径以及要生成的模式)。

由于崩溃仅在应用程序开始运行(并生成至少一张图表)后关闭 OOo 时发生,因此这避免了整个问题。这是一个非常丑陋的黑客,但它完成了工作。

I was never able to figure this out, but I did find a way to hack around it:

I created a separate executable to do the generating. My main application then calls this executable, passing it the necessary parameters (just a path to the file to generate from, and the mode to generate in).

Since the crash only occurs when OOo has been closed since the application started running (and generated at least one chart), this avoids the whole issue. It's a very ugly hack, but it gets the job done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文