GDI+ Generic Exceptions and AccessViolationException observed when running rendering in multiple threads for a long period of time

Hi,



When running excel rendering on multiple threads, AccessViolationException and Aspose.Cells.CellsException: A generic error occurred in GDI+. might happen



Code:



private static int Main(string[] arguments)

{

//Need to use a licenced version to run Aspose in multiple threads.

RegisterAspose();



CellsHelper.DPI = 96;



Thread thread = new Thread(() => Run(0));

thread.Start();

Thread thread1 = new Thread(() => Run(1));

thread1.Start();

Thread thread2 = new Thread(() => Run(2));

thread2.Start();

Thread thread3 = new Thread(() => Run(3));

thread3.Start();

Thread thread4 = new Thread(() => Run(4));

thread4.Start();

Thread thread5 = new Thread(() => Run(5));

thread5.Start();

Thread thread6 = new Thread(() => Run(6));

thread6.Start();

Thread thread7 = new Thread(() => Run(7));

thread7.Start();



return 0;

}



public static void Run(int index)

{

Assembly executingAssembly = Assembly.GetExecutingAssembly();

string path = Path.GetDirectoryName(executingAssembly.Location);

int i = 1;

while (true)

{

Console.WriteLine(i++);

Thread.Sleep(500);

var book = new Workbook(path + @"\test" + index + “.xlsx”);

Worksheet sheet = book.Worksheets[0];

var options = new ImageOrPrintOptions

{

ImageFormat = ImageFormat.Emf,

OnePagePerSheet = true

};

sheet.PageSetup.PrintArea = “B5:E9”;

SheetRender sr = new SheetRender(sheet, options);

sr.ToImage(0, path + @"\out" + index + “.emf”);

}

}



After running it for a long period of time (1 hour to 12 hours in my record) I can see the process crashed with one of the errors:



Aspose.Cells.CellsException: A generic error occurred in GDI+.

at .(Int32 , Stream , ImageOrPrintOptions )

at .(Int32 , ImageOrPrintOptions , Stream )

at .(Int32 , String , ImageOrPrintOptions )

at Aspose.Cells.Rendering.SheetRender.ToImage(Int32 pageIndex, String fileName)



Or



Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is ofen an idication that other memory is corrupt.

at System.Drawing.SafeNativeMethods.Gdip.GdipGraphicsClear(HanlerRef graphics, Int32 argb)

at System.Drawing.Graphics.Clear(Color color)

at .( , Stream , , ImageOrPrintOptions )

at .(Int32 , Stream , ImageOrPrintOptions )

at .(Int32 , ImageOrPrintOptions , Stream )

at .(Int32 , String , ImageOrPrintOptions )

at Aspose.Cells.Rendering.SheetRender.ToImage(Int32 pageIndex, String fileName)





Could you please look into this?



Thanks

Hi,

Thanks for providing us sample files and details.

See the thread for your reference (similar problem occured there too): <a rel="nofollow" href="https://forum.aspose.com/t/91633</a></div><div><br></div><div>Well, there are too many GDI objects being used in ToImage() method. I am afraid, we cannot add a lock for each of them. We think the best way to cope with it is to add a lock to ToImage() method.</div><div><br></div><div>Thank you.</div>