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.

Tue Nov 18, 2014 5:01 pm

Hello,
I have a static function that is called by many threads, this function simply saves a spire document(doc,docx...) as a pdf document.
If there is one thread, no problem with generation pdf, but when there are many threads, i have issues like :
collection has been modified..
object reference not set to an instance of an object.
...
I see that many users have the same problem, i dont know if you have a solution to handle multi threading when generating pdf.

Thank you.

tarikzaid
 
Posts: 29
Joined: Wed Aug 06, 2014 7:16 am

Wed Nov 19, 2014 6:18 am

Hello,

Thanks for your inquiry.
I tried the following code to convert word document to PDF in multi threading but didn't reproduce your issuse.
Would you please share your code to help us reproduce it?
Code: Select all
 private void button1_Click(object sender, EventArgs e)
        {
            List<Thread> threads = new List<Thread>();
            DateTime t0 = DateTime.Now;
            for (int i = 0; i < 10; i++)
            {
                string filname = "result" + i.ToString() + ".pdf";

                Thread thread = new Thread(() =>
                {
                    ConvertWordToPDF(filname);
                });
                thread.Start();
                threads.Add(thread);
            }
            threads.ForEach(t => t.Join());
            Close();
        }
        private void ConvertWordToPDF(string filename)
        {
            Document doc = new Document();
            doc.LoadFromFile("..\\..\\Word.doc");
            doc.SaveToFile(filename,FileFormat.PDF);
            doc.Close();
        }


Thank you.

Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF