Open dylanvdmerwe opened 4 years ago
A rudimentary, but works, way to handle this would be to lock the Convert
method in ConvertWithLibreOffice
.
private static readonly object processLock = new object();
public static void Convert(string inputFile, string outputFile, string libreOfficePath)
{
lock (processLock)
{
List<string> commandArgs = new List<string>();
string convertedFile = "";
if (libreOfficePath == "")
{
libreOfficePath = GetLibreOfficePath();
}
and remove this code:
//Supposedly, only one instance of Libre Office can be run simultaneously
while (pname.Length > 0)
{
Thread.Sleep(5000);
}
Note that this will fix it for one process. But if you have multiple processes using this library (i.e. on a server) and both need to use libreoffice to generate files, you will have errors as it does not seem multiple libreoffices can be started at the same time.
Are we using the right LibreOffice params? Surely there's a way to run multiple conversions (LibreOffice processes) at the same time?
Dang - that is a very valid point, which we have not yet considered!! If you have any time to check this out, it would be great. Then you could make a pull request and I could integrate it!! Best Martin
of course this line of code
//Supposedly, only one instance of Libre Office can be run simultaneously
while (pname.Length > 0)
{
Thread.Sleep(5000);
}
is a bug. After waiting for 5 seconds the array pname
still has the same content, even if meanwhile the process of soffice.exe has exitet. you have to requery again every 5 seconds:
//Supposedly, only one instance of Libre Office can be run simultaneously
while (pname.Length > 0)
{
Thread.Sleep(5000);
pname = Process.GetProcessesByName("soffice");
}
Describe the bug I wanted to inquire if there had been any tests around multi threaded safety with using this library?
It is obviously starting a process for LibreOffice, but this begs to raise the question: what would happen if multiple
Convert from DOCX to PDF
operations had to happen in a server environment at the same time?I think this needs to be properly semaphored if only one LibreOffice can run at a time.
I tried with the following modifications to the example provided: