0

i run two therad , each thread process it's pdf, and each thread thread has it's own GhostscriptProcessor, if i just start one thread there is no problem ,but if i start tow thread, gohstScript.net will give error "error occured when call to 'gsapi_new_instance' is made: -100" and i try version gs64bit and version gs32bit , the result is the same

my code as fellows can anyone help me? thanks a lot.

 Thread t1 = new Thread(processPdf);
 Thread t2 = new Thread(processPdf);

 t1.Start("D:\\T1.PDF");

 t2.Start("D:\\T2.PDF");


  public static void processPdf(object obj) {

        GhostscriptVersionInfo gvi = null;
        GhostscriptProcessor   ghostscript = null;
        try
        {

  gvi = 
  GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | 
  GhostscriptLicense.AFPL, GhostscriptLicense.GPL);
            ghostscript = new GhostscriptProcessor(gvi);

            string outputPath = obj.ToString();
            string input = null;

            if (outputPath.Contains("T1")) {
                input = @"D:\TestFiles\111.pdf";
            }
            else {
                input = @"D:\TestFiles\222.pdf";
            }
            string[] args = GetArgs(input, outputPath);              

            ghostscript.Process(args);

        }
        catch (Exception ex) {

            Console.WriteLine(ex.StackTrace+"|"+ex.Message);

        }

    }//多线程

 private static string[] GetArg(string inputFile, string outputFile)
  {
         return new[] {
         $"gs",
         $"-o",
         $"{outputFile}",
         $"-dNoOutputFonts",   
         $"-sDEVICE=pdfwrite",
         $"{inputFile}",
        };
  }
steive
  • 11
  • 5

1 Answers1

1

You have to build the Ghostscript DLL in a specific way in order to make it thread-safe, which is not the way the standard DLL is built.

If you try to use a non-thread safe DLL with two threads it will indeed refuse to start the second instance adnd will return gs_error_Fatal (-100).

You can do this, but you need to rebuild the DLL, you need to define the compiler flag GS_THREADSAFE. Be advised; if you do that some devices will stop working, they are inherently not thread-safe. These are 'contributed' devices which Artifex does not own the copyright for and is therefore unable to modify.

KenS
  • 30,202
  • 3
  • 34
  • 51