Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Nov 19, 2014 5:23 pm

Hi,

I have experienced problems when using spire with multi-threaded application.
the application has been running successfully as a single threaded application. As soon as the multi threading was implemented, spire has been throwing the following errors:

•Wrong dynamic huffman codes.
•Object reference not set to an instance of an object.
•Unknown block type. Stream might be corrupted.
•Zip exception.Can't find local header signature - wrong file format or file is corrupt.
•Zip exception.End of file reached - wrong file format or file is corrupt.
•Zip exception.Unable to read value at the specified position - end of stream was reached.
•Zip exception.Wrong Crc value.

If I put the call to for example LoadFromStream or SaveToStream within a lock statement everything works, indicating that the errors are somehow caused by lack of thread safety..?

For example, i have a function that converts a stream to pdf, when it is called by several threads, it throws the exceptions below :
Code: Select all
public  byte[] ConvertToPdf(byte[] content)
        {
            byte[] result = null;
            CultureInfo cc = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            try
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                Document doc = new Document();
               
                    using (MemoryStream str = new MemoryStream(content))
                    {
                        doc.LoadFromStream(str, FileFormat.Docx);
                    }
                    ToPdfParameterList topdfparams = new ToPdfParameterList();
                    topdfparams.IsEmbeddedAllFonts = true;
                    topdfparams.AutoFitTableLayout = true;
                    using (MemoryStream outstream = new MemoryStream())
                    {
                        doc.SaveToStream(outstream, topdfparams);
                        doc.Close();
                        result = outstream.ToArray();
                    }
                    return result;
            }
            catch (Exception ex)
            {
                throw new AsConfException("Can not generate pdf : " + ex.Message, "");
            }
             finally
             {
                 Thread.CurrentThread.CurrentCulture = cc;
             }
        }

Any help with this would be much appreciated!

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

Thu Nov 20, 2014 3:25 am

Hello,

Thanks for your inquiry.
I have reproduced the issue and posted it to our dev team. We will tell you when it is resolved.

Best wishes,
Amy
E-iceblue support team
User avatar

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

Thu Nov 27, 2014 8:33 am

Hello,

Thanks for waiting.
The issue has been resolved. Please test new version and the code snippet as below.
http://www.e-iceblue.com/downloads/hot_ ... _5.3.3.zip

Code: Select all
static void Main(string[] args)
        {
            string inputFile = @"..\\..\\Word.docx";

            string outputPdf = @"result{0}.pdf";

            List<Thread> threads = new List<Thread>();
            byte[] data = File.ReadAllBytes(inputFile);
            for (int i = 0; i < 10; i++)
            {
                MemoryStream ms = new MemoryStream(data);
                string filename = string.Format(outputPdf, i);
                MyThread mythread = new MyThread(filename, ms);
                ThreadStart threadStart = new ThreadStart(mythread.ConvertToPdf);
                Thread thread = new Thread(threadStart);
                threads.Add(thread);
            }

            threads.ForEach(t => t.Start());
        }
        public class MyThread
        {
            private string m_fileName;
            private Stream m_content;
            public static object myThreadObj = new object();

            public string FileName
            {
                get
                {
                    return m_fileName;
                }
                set
                {
                    m_fileName = value;
                }
            }

            public Stream Content
            {
                get
                {
                    return m_content;
                }
                set
                {
                    m_content = value;
                }
            }

            public MyThread(string pdfName, Stream content)
            {
                m_fileName = pdfName;
                m_content = content;
            }

            public void ConvertToPdf()
            {
                lock (myThreadObj)
                {
                    CultureInfo cc = Thread.CurrentThread.CurrentCulture;
                    Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                    try
                    {
                        Document doc = new Document();
                        Content.Position = 0L;
                        doc.LoadFromStream(Content, FileFormat.Docx2010);

                        #region Svae to pdf code
                        ToPdfParameterList topdfparams = new ToPdfParameterList();
                        topdfparams.IsEmbeddedAllFonts = true;
                        topdfparams.AutoFitTableLayout = true;
                        using (MemoryStream outstream = new MemoryStream())
                        {
                            doc.SaveToStream(outstream, topdfparams);
                            File.WriteAllBytes(FileName, outstream.ToArray());
                            //result = outstream.ToArray();
                        }
                        #endregion


                        doc.Close();
                        //return result;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Can not generate pdf : " + ex.Message);
                    }
                    finally
                    {
                        Thread.CurrentThread.CurrentCulture = cc;
                    }
                }
            }
        }


Best wishes,
Amy
User avatar

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

Mon Dec 01, 2014 9:15 am

Hello,

Have you tested spire.doc_5.3.3? Did it resolve your issue?
Thanks for your feedback.

Best wishes,
Amy
E-iceblue support team
User avatar

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

Return to Spire.Doc