Bootstrap Exception in CSharp
Posted: Sat Dec 22, 2012 10:19 am
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
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";
}
}