Bootstrap Exception in CSharp

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
huyan200599
Posts: 7
Joined: Sat Dec 22, 2012 10:08 am

Bootstrap Exception in CSharp

Post by huyan200599 »

dear all
I had a problem, the following code in the execution 1-190 times to do not have a problem, but in between 190-210 will always be the error, can you tell me how to solve? The following is my code.
the exception in unoidl.com.sun.star.uno.XComponentContext xLocalContext = uno.util.Bootstrap.bootstrap();this line .
Please tell me how to solve this~
3Q

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Diagnostics;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.beans;
using System.Net.Sockets;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;

namespace TestOpenOffice
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintPdf();
            Console.ReadKey();
        }

        private static void PrintPdf()
        {
            for (var i = 0; i < 300; i++)
            {
                GeneratePdf(i);
            }
        }

        private static void GeneratePdf(int i)
        {
            XComponent xComponent = null;
            string _sourcePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "generate", FileList.doc);
            string _destinationPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "generate",
                string.Format("{0}.pdf", Guid.NewGuid().ToString()));
            try
            {
                // 載入文件前屬性設定,設定文件開啟時隱藏  
                PropertyValue[] loadDesc = new PropertyValue[1];
                loadDesc[0] = new PropertyValue();
                loadDesc[0].Name = "Hidden";
                loadDesc[0].Value = new uno.Any(true);

                //Get a ComponentContext
                unoidl.com.sun.star.uno.XComponentContext xLocalContext = uno.util.Bootstrap.bootstrap();

                //Get MultiServiceFactory
                unoidl.com.sun.star.lang.XMultiServiceFactory xRemoteFactory = (unoidl.com.sun.star.lang.XMultiServiceFactory)xLocalContext.getServiceManager();

                //Get a CompontLoader
                XComponentLoader aLoader = (XComponentLoader)xRemoteFactory.createInstance("com.sun.star.frame.Desktop");

                //Load the sourcefile
                xComponent = aLoader.loadComponentFromURL(PathConverter(_sourcePath), "_blank", 0, loadDesc);

                //Wait for loading
                while (xComponent == null)
                {
                    System.Threading.Thread.Sleep(3000);
                }

                saveDocument(xComponent, PathConverter(_destinationPath));

                xComponent.dispose();



            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Process[] pt = Process.GetProcessesByName("soffice.bin");
                if (pt != null && pt.Length > 0)
                    pt[0].Kill();

                Process[] ps = Process.GetProcessesByName("C:\\Program Files\\OpenOffice.org 3\\program\\soffice.exe");
                if (ps != null && ps.Length > 0)
                    ps[0].Kill();

                //if (obtained)
                //    _openOfficeLock.ReleaseMutex();
            }
        }

        /// <summary>
        /// Saves the document.
        /// </summary>
        /// <param name="xComponent">The x component.</param>
        /// <param name="fileName">Name of the file.</param>
        private static void saveDocument(XComponent xComponent, string fileName)
        {
            unoidl.com.sun.star.beans.PropertyValue[] propertyValue = new unoidl.com.sun.star.beans.PropertyValue[1];

            propertyValue[0] = new unoidl.com.sun.star.beans.PropertyValue();
            propertyValue[0].Name = "FilterName";
            propertyValue[0].Value = new uno.Any("writer_pdf_Export");

            ((XStorable)xComponent).storeToURL(fileName, propertyValue);
        }

        /// <summary>
        /// Convert into OO file format
        /// </summary>
        /// <param name="file">The file.</param>
        /// <returns>The converted file</returns>
        private static string PathConverter(string file)
        {
            try
            {
                file = file.Replace(@"\", "/");

                return "file:///" + file;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }


    }

    public class FileList
    {
        public static string doc = "GAIA_查询中心_PIS_old.doc";
        public static string docx = "GAIA 7.0.0 Portal MRS V1.0.docx";
        public static string pptx = "GAIA_UIStyle_20120406.pptx";
        public static string ppt = "GAIA_UIStyle_20120406_t.ppt";
        public static string xls = "无微不至网络传媒(微博)套餐报价表A.xls";
        public static string xlsx = "一年级.xlsx";
        public static string wps = "afdsafdsaf.wps";
        public static string dps = "a.dps";
        public static string et = "Book1.et";
        public static string txt = "baidu.upload.txt";
    }
}
OpenOffice 3.4 windows xp
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: bootstrap Exception in CSharp

Post by RoryOF »

Consider a memory leak, where shortage of memory reaches critical proportions after 190+ iterations of your code.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
huyan200599
Posts: 7
Joined: Sat Dec 22, 2012 10:08 am

Re: bootstrap Exception in CSharp

Post by huyan200599 »

RoryOF wrote:Consider a memory leak, where shortage of memory reaches critical proportions after 190+ iterations of your code.
how to do this? Thread.Sleep(1000);?
but when i restart the application the bootstrap is ok~
if the code in a service, the service cannot restart.
can u give me a sample for Csharp to generate pdf more then 200 times and no exception?
OpenOffice 3.4 windows xp
huyan200599
Posts: 7
Joined: Sat Dec 22, 2012 10:08 am

Re: bootstrap Exception in CSharp

Post by huyan200599 »

RoryOF wrote:Consider a memory leak, where shortage of memory reaches critical proportions after 190+ iterations of your code.
and anthor question for the memory leak, the 190+ times is the open issue for openoffice3.4.1?
OpenOffice 3.4 windows xp
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: bootstrap Exception in CSharp

Post by RoryOF »

huyan200599 wrote:
RoryOF wrote:Consider a memory leak, where shortage of memory reaches critical proportions after 190+ iterations of your code.
how to do this? Thread.Sleep(1000);?
but when i restart the application the bootstrap is ok~
if the code in a service, the service cannot restart.
can u give me a sample for Csharp to generate pdf more then 200 times and no exception?
You need to monitor memory usage using some form of monitoring utility. If you are writing low level code you should find out about debugging.

With regard to your email to me, on the Forum we deal with all applications through publicly posted messages and replies, not off-channel.

No, I cannot give an example of Csharp code to do what you request, as I do not program at that level, but my instinct is that the first culprit for your code to fail after so many iterations is most likely a memory leak. How you follow this suggestion is for you to decide.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
huyan200599
Posts: 7
Joined: Sat Dec 22, 2012 10:08 am

Re: bootstrap Exception in CSharp

Post by huyan200599 »

RoryOF wrote:
huyan200599 wrote:
RoryOF wrote:Consider a memory leak, where shortage of memory reaches critical proportions after 190+ iterations of your code.
how to do this? Thread.Sleep(1000);?
but when i restart the application the bootstrap is ok~
if the code in a service, the service cannot restart.
can u give me a sample for Csharp to generate pdf more then 200 times and no exception?
You need to monitor memory usage using some form of monitoring utility. If you are writing low level code you should find out about debugging.

With regard to your email to me, on the Forum we deal with all applications through publicly posted messages and replies, not off-channel.

No, I cannot give an example of Csharp code to do what you request, as I do not program at that level, but my instinct is that the first culprit for your code to fail after so many iterations is most likely a memory leak. How you follow this suggestion is for you to decide.
how to monitor memory? which the object i should monitor it?
OpenOffice 3.4 windows xp
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Bootstrap Exception in CSharp

Post by RoryOF »

Turn on TaskManager (Ctrl shft ESC); this is a built-in monitor for processes and will do to start with. Thre are more specialised tools which Google will find, but start with this one. On its Processes tab you will see the memory consumed by each running process.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
huyan200599
Posts: 7
Joined: Sat Dec 22, 2012 10:08 am

Re: Bootstrap Exception in CSharp

Post by huyan200599 »

RoryOF wrote:Turn on TaskManager (Ctrl shft ESC); this is a built-in monitor for processes and will do to start with. Thre are more specialised tools which Google will find, but start with this one. On its Processes tab you will see the memory consumed by each running process.
sorry i means that which object in ooo if i monitoring it can let me resolved this bug?
OpenOffice 3.4 windows xp
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Bootstrap Exception in CSharp

Post by RoryOF »

In TaskMaster observe the memory in use by soffice processes; this may increase for each iteration of your code until the code fails. If so you have to determine why; perhaps this might be due to an internal flaw in OpenOffice (the mempry leak I referred to), for which there might not be a fix other than restarting your code after a safe number of iterations, or due to a flaw caused by failure of your code to release its housekeeping spaces, which might require insertion of housekeeping events in your code sequence ("garbage collection") - see this reference for some hints
http://stackoverflow.com/questions/2764 ... et-c-sharp
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
huyan200599
Posts: 7
Joined: Sat Dec 22, 2012 10:08 am

Re: Bootstrap Exception in CSharp

Post by huyan200599 »

RoryOF wrote:In TaskMaster observe the memory in use by soffice processes; this may increase for each iteration of your code until the code fails. If so you have to determine why; perhaps this might be due to an internal flaw in OpenOffice (the mempry leak I referred to), for which there might not be a fix other than restarting your code after a safe number of iterations, or due to a flaw caused by failure of your code to release its housekeeping spaces, which might require insertion of housekeeping events in your code sequence ("garbage collection") - see this reference for some hints
http://stackoverflow.com/questions/2764 ... et-c-sharp
i have used the using grammar, but the vs 2010 tip me:"the unoidl.com.sun.star.uno.XComponentContext do not support the interface of IDispose" ~
OpenOffice 3.4 windows xp
huyan200599
Posts: 7
Joined: Sat Dec 22, 2012 10:08 am

Re: Bootstrap Exception in CSharp

Post by huyan200599 »

RoryOF wrote:In TaskMaster observe the memory in use by soffice processes; this may increase for each iteration of your code until the code fails. If so you have to determine why; perhaps this might be due to an internal flaw in OpenOffice (the mempry leak I referred to), for which there might not be a fix other than restarting your code after a safe number of iterations, or due to a flaw caused by failure of your code to release its housekeeping spaces, which might require insertion of housekeeping events in your code sequence ("garbage collection") - see this reference for some hints
http://stackoverflow.com/questions/2764 ... et-c-sharp
I want to say, every time I open the soffice exe. This program after conversion, there are timely turn it off, but in the bootstrap to 200th time will still error, excuse me this problem have others reaction? If the openoffice open issued?
OpenOffice 3.4 windows xp
Post Reply