PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Error when running MigraDoc in asynchronous Task
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3061
Page 1 of 1

Author:  D_F_G [ Mon Feb 23, 2015 10:06 am ]
Post subject:  Error when running MigraDoc in asynchronous Task

Hello,

when I try to create a PDF in an asynchronous Task I get the following Exception:

Code:
Der Thread wurde abgebrochen.
   bei Microsoft.Win32.Win32Native.WriteFile(SafeFileHandle handle, Byte* bytes, Int32 numBytesToWrite, Int32& numBytesWritten, IntPtr mustBeZero)
   bei System.IO.FileStream.WriteFileNative(SafeFileHandle handle, Byte[] bytes, Int32 offset, Int32 count, NativeOverlapped* overlapped, Int32& hr)
   bei System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
   bei System.IO.FileStream.FlushInternalBuffer()
   bei System.IO.FileStream.Flush(Boolean flushToDisk)
   bei System.IO.FileStream.Flush()
   bei System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   bei System.IO.StreamWriter.Flush()
   bei MigraDoc.DocumentObjectModel.Serializer.get_Position()
   bei MigraDoc.DocumentObjectModel.Serializer.BeginBlock()
   bei MigraDoc.DocumentObjectModel.Serializer.BeginContent()
   bei MigraDoc.DocumentObjectModel.Tables.Cell.Serialize(Serializer serializer)
   bei MigraDoc.DocumentObjectModel.Tables.Cells.Serialize(Serializer serializer)
   bei MigraDoc.DocumentObjectModel.Tables.Row.Serialize(Serializer serializer)
   bei MigraDoc.DocumentObjectModel.Tables.Rows.Serialize(Serializer serializer)
   bei MigraDoc.DocumentObjectModel.Tables.Table.Serialize(Serializer serializer)
   bei MigraDoc.DocumentObjectModel.DocumentElements.Serialize(Serializer serializer)
   bei MigraDoc.DocumentObjectModel.Section.Serialize(Serializer serializer)
   bei MigraDoc.DocumentObjectModel.Sections.Serialize(Serializer serializer)
   bei MigraDoc.DocumentObjectModel.Document.Serialize(Serializer serializer)
   bei MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteDocument(DocumentObject documentObject)
   bei MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(DocumentObject docObject, String filename, Int32 indent, Int32 initialIndent)
   bei MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(DocumentObject docObject, String filename)


What I call is:

Code:
Task t = Task.Factory.StartNew(
      c =>
      {
        try
        {
           //generates the pdf and calls the function that throws the Exception
          sendPdfReport((HttpContext)c);
        }
        catch (Exception e)
        {
          //Do something in case of Exception
        }
      }, HttpContext.Current);

Author:  Thomas Hoevel [ Mon Feb 23, 2015 10:37 am ]
Post subject:  Re: Error when running MigraDoc in asynchronous Task

Hi!

The call stack shows "DdlWriter.WriteToFile". Are you sure you're creating a PDF file, not an MDDDL file?
Maybe there is some debug code that creates an MDDDL file.

Author:  D_F_G [ Mon Feb 23, 2015 12:51 pm ]
Post subject:  Re: Error when running MigraDoc in asynchronous Task

Hi,

as I took from the examples, first I create a MDDDL file then I create the pdf.
Isn't it necessary to create the MDDDL?

Author:  Thomas Hoevel [ Mon Feb 23, 2015 1:54 pm ]
Post subject:  Re: Error when running MigraDoc in asynchronous Task

D_F_G wrote:
Isn't it necessary to create the MDDDL?
This step is not necessary.
Our application creates MDDDL files in DEBUG mode as it might help if the resulting document does not look as expected, but in RELEASE build we never create them.
We'll make this clearer in the samples when we revise them.

The exception occurs during file IO - and creation of the PDF also requires file IO, so the same exception may occur.
You can use a memory stream to create the PDF "file".

Is there a critical race maybe? Task still running while HttpContext was already disposed or something like that?

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/