Creating workbook object from Excel template MemoryStream in multi threading environment in .NET

Getting “The file is corrupted” exception when creating workbook object from Excel Template memoryStream in multi threading environment.
For single file it is working fine but, when I am trying to create it for multiple multiple files using threading it is throwing this exception.

Appreciate the help.

@sachingangarde,

Thanks for your query.

For your information, Aspose.Cells is a pure .NET component created in managed C#, so , concurrency and multithreading should not be a problem by any means. Our several users do use the component in their diverse environments in different types of (desktop, client server etc.) solutions (Winforms, web forms, windows/web service, console app. etc.) or other scenarios without any problem. As long as you don’t have shared data source and every time a user access the application a new Workbook or Excel file is generated, there would be no problem at all. However, if you have a shared data/resource, then you will have to do synchronization by yourself. Also, in that case, we recommend you to create/manipulate of filling data into different workbooks in different threads accordingly, because, you should not use one workbook/file in multiple threads at the same time least you would not get stable data due to restrictions and complexity (involved) put forth by MS Excel file format(s).

@Amjad_Sahi: Thanks for the response.

I am starting separate thread for each workbook, not multiple threads on single workbook. The only Shared thing is Excel template’s stream and there it gives error when running in multi threading mode.

With sequential processing there is no issue at all.

@sachingangarde,
I have tried my sample code but could not observe any issue as it executes without any error and generates 500 files successfully. You may please provide us your simplified code along with the template file for our testing. We will reproduce the issue here and provide assistance accordingly.

@sachingangarde,

Please note, of course the stream cannot be shared between different threads and reading data from it concurrently should not be done. When reading data from the stream, the pointer of the stream is surely moved by the reading process separately. It is very possible that while thread “A” is reading the data block at position “a” from the stream, thread “B” moves the stream pointer to “b” and then thread “A” cannot get the proper data anymore.

Thanks for your understanding!

Thanks for the response along with the example.

The above example won’t give any problem as there is nothing in the stream. I also followed similar example and ended up into the problem.

Thing is, if we have stream for the template, it gives problem while using in multi threading environment as stream is not thread safe but, it is nowhere mentioned on Aspose blogs/support and above example also the same.

Anyways, I have figured out the solution for the problem.

@Amjad_Sahi Thanks for the response. Please see the example posted by @ahsaniqbalsidiqui. I followed it exactly same way and ended up into a problem.

Basically, stream is not thread safe to use in multi threading environment, but still your support shows unsafe usage examples. This is the problem.

Anyways, I have figured out the solution for this problem.

@sachingangarde,

Sorry for the confusion. You are right. The example (shared) is not right, either we will update it or remove it. Well, MemoryStream is not documented as thread safe, so you have to make sure that you will access this instance from one thread at a time. Moreover, you have to make it thread-safe (synchronized) using your own code by yourself (e.g see the page for your reference).

Also, good to know that you have sorted it out now.

@Amjad_Sahi: I would suggest to update an example.

@sachingangarde,
Thanks for suggestion. We will further look into it.