Multi thread cause failed image generating

Hi,
Please try the following code. My laptop configuration (MacOS High Serria 8 cores, 16GB RAM)

var ms = new FileStream("/tmp/8MB.xlsx", FileMode.OpenOrCreate, FileAccess.Read);
_wb = new Workbook(ms);

		for(int i = 0;i<3;i++)
		{
			Thread th = new Thread(new ThreadStart(()=>{
				Stopwatch s = new Stopwatch();
				s.Start();
				Worksheet worksheet = _wb.Worksheets[0];
				worksheet.PageSetup.PrintArea = "A1:I200";

				// Set all margins as 0
				worksheet.PageSetup.LeftMargin = 0;
				worksheet.PageSetup.RightMargin = 0;
				worksheet.PageSetup.TopMargin = 0;
				worksheet.PageSetup.BottomMargin = 0;

				// Set OnePagePerSheet option as true
				ImageOrPrintOptions options = new ImageOrPrintOptions();
				options.OnePagePerSheet = true;
				options.ImageType = Aspose.Cells.Drawing.ImageType.Png;
				options.OnlyArea = true;
				//options.HorizontalResolution = 90;
				//options.VerticalResolution = 90;
				SheetRender sr = new SheetRender(worksheet, options);
				using (var msImg = new MemoryStream())
				{
					sr.ToImage(0, msImg);
					//Console.WriteLine(Convert.ToBase64String(msImg.ToArray()));
					Convert.ToBase64String(msImg.ToArray());
				}
				s.Stop();
				Console.WriteLine(s.ElapsedMilliseconds);
			}));
			th.Start();
		}

When you increase the concurrent threads, it will throw error.

Don’t know whether it was aware? If not could you provide fix or we have to lock it for reading?

@Kevinng85,

I have noticed CellsException with your code. If you work with single workbook’s data by opening it concurrently in multi-threads, the result might be unstable. So, the best way to utilize multi-threading is to use separate Workbook objects in each thread.

Moreover, could you please share exception details and your template file for our analysis.

Thanks @ahsaniqbalsidiqui

Actually we bypass the error by exclusively lock the workbook by Id.
However, it’s good if you guys can take a look as I guess printing operation should be thread-safe (especially this is must read-only).
You can replicate the error with the code I posted above (any excel file should be fine)

Feel free to close this topic.

@Kevinng85,

We have logged the issue in our database for detailed investigations. Once, we will have some news for you, we will update you in this topic.

This issue has been logged as

CELLSNET-46516 - Multi threading cause failed image generation
1 Like

@Kevinng85,

We evaluated your issue further. When performing rendering for the workbook, or even one single worksheet, there are still many global caches which are needed to be initialized or rebuilt, such as, the formatting rules according with the settings of the Workbook. Modifications to such kind of global data will cause concurrency issues in multi-threaded environment. Take the formatting relevant data as example again, it is the basic requirement for many other features, such as, getting the formatted result of one cell. So, there are many functions which may cause the global data cache initialized or changed. For safety measures, you should always try to avoid to process one workbook in multiple threads.

Thanks for your understanding!