Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Aug 07, 2015 3:18 pm

Hi!

I'm already aware of the fact that PrintDocument.Print() is not threadsafe. To circumvent this problem I'm doing the printing in a separate thread. Unfortunately this does not help. I'm getting an ArgumentException:

Code: Select all
System.ArgumentException: Ein Element mit dem gleichen Schlüssel wurde bereits hinzugefügt.
bei System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
bei System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
bei sprᮌ.ᜂ()
bei sprᮌ.ᜁ(Single A_0)
bei sprᮌ.ᜂ(Single A_0)
bei sprᲅ.ᜀ(Graphics A_0, spr᫊ A_1, Boolean A_2)
bei sprᲅ.ᜁ(Graphics A_0, spr᫊ A_1, Boolean A_2)
bei spr᫊.ᜀ(String A_0, sprᮌ A_1, String A_2, Boolean A_3)
bei spr᫊.ᜀ(String[] A_0, String A_1)
bei spr᫊.ᜀ(sprᰡ A_0)
bei spr᫊.ᜇ(Boolean A_0)
bei spr᫊.ᜟ()
bei Spire.Pdf.PdfDocument.ᜀ(Object A_0)
bei Spire.Pdf.PdfDocument.OnPrintPage(Object sender, PrintPageEventArgs e)
bei System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e)
bei System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e)
bei System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)
bei System.Drawing.Printing.PrintController.Print(PrintDocument document)
bei System.Drawing.Printing.PrintDocument.Print()
bei MobileIQ.PrintService.ConsumerService.Tools.Printer.<>c__DisplayClass1.<Print>b__0()
bei MobileIQ.PrintService.ConsumerService.Tools.StaTasks.<>c__DisplayClass4.<StartStaTask>b__3()


I have a german Windows, hence the german error message. The translation is:

Code: Select all
Argument Exception: Item with Same Key has already been added


This means for me: Either I did something wrong in my code or running the Print method in a different thread is not sufficient.

My Question: How can I print a PDF document with Spire.PDF in a threadsafe way? Is it even possible?

I'm usung Spire.PDF 3.4.27.

Best Regards
Olaf


Here are the relevant code snippets:

Code: Select all
   
    public static void Print(string printerName, string document)
    {
        var task = StaTasks.StartStaTask(() =>
        {
            using (var doc = new PdfDocument())
            {
                doc.LoadFromFile(document);
                doc.PrinterName = printerName;

                var printDoc = doc.PrintDocument;
                printDoc.Print();
            }
        });
        task.Wait();
    }


Code: Select all
   
    public static Task StartStaTask(Action action)
    {
        var tcs = new TaskCompletionSource<object>();

        var thread = new Thread(() =>
        {
            try
            {
                action();
                tcs.SetResult(null);
            }
            catch (Exception e)
            {
                tcs.SetException(e);
            }
        });

        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();

        return tcs.Task;
    }

olaf2
 
Posts: 8
Joined: Fri Aug 07, 2015 3:09 pm

Mon Aug 10, 2015 9:32 am

Hi Olaf,

Thanks for inquiry.

Multi-thread issue has been repaired, please download the new hotfix from the link blow and have a try.
http://www.e-iceblue.com/downloads/TempVersion/spire.pdf_3.4.143.zip

Wish you have a nice day. If you have any question, welcome to let us know.

Best regards,
Simon
E-iceblue support team
User avatar

Simon Liang
 
Posts: 8
Joined: Thu Jul 23, 2015 4:43 am

Mon Aug 10, 2015 3:51 pm

Hi Support Team!

Thank you very much for your immediate answer.

I just exchanged Spire.PDF 3.4.27 with the Hotfix (3.4.143). Unfortunately I can not see any improvement. I still get the same error.

Best Regards
Olaf

olaf2
 
Posts: 8
Joined: Fri Aug 07, 2015 3:09 pm

Tue Aug 11, 2015 1:33 am

Hi Olaf,

There was no exception when I ran your codes which you provided in my side.
I doubt if it’s convenient to provide us your sample PDF document here, or you can send it to us ([email protected]) via email.
It is helpful for us to have a better investigation.

Best regards,
Simon

E-iceblue support team
User avatar

Simon Liang
 
Posts: 8
Joined: Thu Jul 23, 2015 4:43 am

Tue Aug 11, 2015 9:49 am

Hi Support Team!

I build two test solutions: a simple console application and a console application which hosts a OWIN web app. In both applications I have included my code so that three PDF documents are printed in parallel in separate threads. It worked fine. No exceptions.

Only in my real application I still got the exceptions mentioned before. In sheer desperation I put some logging in the catch block to verify that the printing is done really in a separate thread. And that thread is really running in STA mode. The logging output tells me that everything looks fine:

Code: Select all
Drucker = [DRC00193_Local], Dokument = [C:\tmp\Upload\Printjob_20150811-112943_3ab60a5b-c6b1-4929-b224-67c20008b862.pdf], Process Id = [17976], Thread Id = [12], ApartmentState = [STA]

Drucker = [DRC00193_Local], Dokument = [C:\tmp\Upload\Printjob_20150811-112941_7b877396-74be-42ff-bea9-39c44ce4671a.pdf], Process Id = [17976], Thread Id = [14], ApartmentState = [STA]


The only difference I can see is that I'm building a Windows Service. My applications listens to an MSMQ queue and reacts to print messages. Maybe Spire.PDF does not operate correctly in a Windows Service context?

Best regards,
Olaf

olaf2
 
Posts: 8
Joined: Fri Aug 07, 2015 3:09 pm

Wed Aug 12, 2015 9:46 am

Hi Olaf,

Thanks for reply.
Could you please provide me a simple windows service project which can reproduce the issue?
It’s helpful to investigate the issue you are facing and it will be better if you attach your source document.
Thanks in advance.

Best regards,
Simon

E-iceblue support team
User avatar

Simon Liang
 
Posts: 8
Joined: Thu Jul 23, 2015 4:43 am

Wed Aug 12, 2015 1:24 pm

Hi Support Team!

I have just sent you a test solution by email. I hope it will help you.

Best regards,
Olaf

olaf2
 
Posts: 8
Joined: Fri Aug 07, 2015 3:09 pm

Wed Aug 19, 2015 6:43 am

Hi Support Team!

I have just tried the new version 3.5. The problems are gone. Thank you.

Best regards,
Olaf

olaf2
 
Posts: 8
Joined: Fri Aug 07, 2015 3:09 pm

Wed Aug 19, 2015 7:30 am

Hi Olaf,

Glad to hear that your issue was resolved.
Please feel free to contact us, if you have any question or request.
We are here for help.

Best regards,
Simon

E-iceblue support team
User avatar

Simon Liang
 
Posts: 8
Joined: Thu Jul 23, 2015 4:43 am

Return to Spire.PDF