Quantcast
Channel: Ghostscript.NET
Viewing all articles
Browse latest Browse all 393

Created Unassigned: GhostScript Interpreter is not multi-threaded on same pdf file [2261]

$
0
0
I've noticed that when GhostscriptRasterizer.GetPage is called for two different pages from the same pdf file at the same time, it typically throws an exception. For example, if you call it this way

```

static void Main()
{
IEnumerable<Task<MagickImage>> images = (from i in Enumerable.Range(1, 10) select GetPageAsync(i));
MagickImage[] tasks = await Task.WhenAll(images);
Console.WriteLine(@"Press enter to exit.");
Console.ReadLine();
}
```
Then this code will work, but only because of the lock:
```

public static readonly GhostscriptVersionInfo LastInstalledVersion =
GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL, GhostscriptLicense.GPL);

public static string PathVar = Path.GetFullPath(@"..\..\..\Pdfs\HP_Thin.pdf");
public static string TestPath = Path.GetFullPath(@"..\..\..\Output\holyPAGE.tif");
public static readonly object SyncRoot = new object();
public static async Task<MagickImage> GetPageAsync(int i)
{
return await Task.Run(() =>
{
const int defaultDpi = 400;
MagickImage image;
Bitmap pdfPage;
lock (SyncRoot)
{
using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
{
rasterizer.Open(PathVar, LastInstalledVersion, false);
pdfPage = (Bitmap)rasterizer.GetPage(defaultDpi, defaultDpi, i);
image = new MagickImage(pdfPage) {Format = MagickFormat.Tif};
pdfPage.Dispose();
}
}
return image;
});
}
```

However, this code doesn't:

```
public static async Task<MagickImage> GetPageAsync(int i)
{
return await Task.Run(() =>
{
const int defaultDpi = 400;
MagickImage image;
Bitmap pdfPage;
using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
{
rasterizer.Open(PathVar, LastInstalledVersion, false);
pdfPage = (Bitmap)rasterizer.GetPage(defaultDpi, defaultDpi, i);
image = new MagickImage(pdfPage) { Format = MagickFormat.Tif };
pdfPage.Dispose();
}
return image;
});
}
```
The stack trace I get is:
```
Ghostscript.NET.GhostscriptAPICallException was caught
HResult=-2146233088
Message=An error occured when call to 'gsapi_new_instance' is made: -100
Source=Ghostscript.NET
Code=-1000
CodeName=""
StackTrace:
at Ghostscript.NET.Interpreter.GhostscriptInterpreter.Initialize()
at Ghostscript.NET.Interpreter.GhostscriptInterpreter..ctor(GhostscriptVersionInfo version, Boolean fromMemory)
at Ghostscript.NET.Viewer.GhostscriptViewer.Open(String path, GhostscriptVersionInfo versionInfo, Boolean dllFromMemory)
at Ghostscript.NET.Rasterizer.GhostscriptRasterizer.Open(String path, GhostscriptVersionInfo versionInfo, Boolean dllFromMemory)
at TestApp.Program.<>c__DisplayClassd.<GetPageAsync>b__c() in c:\src\services\LMS\PdfToTiff\TestApp\Program.cs:line 82
InnerException:
```

Viewing all articles
Browse latest Browse all 393

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>