Multi-threaded excel generation from template - "Index was outside the bounds of the array." error

Hello,

I have 4 parallel threads that do exactly the same thing:
- Instantiate a new workbook and open a template file (same template for the 4 threads):
workbook = new Workbook();
workbook.Open(TemplatePath);
- Fill template sheets with data and a pivot table
- Save the workbook (each thread saves to a distinct file):
workbook.CalculateFormula(true);
workbook.Save(OutputPath, fileFormatType);

When running on 1 CPU, no issue. On a bi-processor server, I get a “Index was outside the bounds of the array.” error. When I run only 1 thread and generate all excel sheets sequentially, no issue, but this does not give a good performance.

Is there a way to solve this ?

One important remark: I do not control the threads: I am embarking my dll in a SSIS (SQL Server Integration Services) package. In the SSIS control flow, I created 4 parallel taks, each tasks calling the report generation method with different parameters. So SSIS is creating the threads itself.

Thanks for your help,

Benjamin


Hi



<span style=“font-size: 12pt; font-family: “Times New Roman”;”>Your request is related to another component (Aspose.Cells). That’s why
I move it to corresponding forum. Their technical support will respond to you
soon.



Best regards.

Hi Benjamin,

We will check and get back to you soon.

Thank you.

Hi Benjamin,

Can you verify if this problem is caused by multi-thread or by your different parameters? And does this problem happen on Open method or Save method?

If the problem happens on Open method, you can try to lock the template file to avoid concurrently opening.

Hi Laurence,

The problem is caused by multi-threading. When I run the batch on 1 CPU server, it works fine. On a 2CPU server, if I generate the 4 reports in parallel, it gives the exception; if I run everything sequentially, it works fine.

The problem happens on the Open method (the 4 threads open the same template).

How could I lock the template file?

Regards,

Benjamin

Hi Benjamin,

Please use lock statement. For detail information, please check your MSDN.

Hi Laurence,

I used the following code and the exception does not occur any more.

Does this code seem ok to you?

Regards,

Benjamin

//Open template

workbook = new Workbook();

//Creating a file stream containing the Excel file to be opened

FileInfo f = new FileInfo(TemplatePath);

lock (f)

{

fstream = new FileStream(TemplatePath, FileMode.Open, FileAccess.Read, FileShare.Read);

workbook.Open(fstream);

}

Hi Benjamin,

I think your code is fine.