RemoveAt failed

Hi,

I have a multithreaded application which allows different threads to work on copy of the same sheet and deletes the sheet before it leaves the thread method.

RemoveAt fails sporadically stating that sheet does not exist or invalidargumentexception even though the worksheet reference is a valid one.

private static Workbook GetWorkbook(string excelPath, out Worksheet ws)
{
string InputOutputSheetName = "IO";
Workbook wb;
lock (FeeLock)
{
if (!ExcelCache.TryGetValue(excelPath, out wb))
{
wb = new Workbook(excelPath);
ExcelCache.Add(excelPath, wb);
}

int index = wb.Worksheets.AddCopy(InputOutputSheetName);
ws = wb.Worksheets[index];
}

return wb;
}

public void Calculate(string excelPath)
{
Worksheet ws;
Workbook wb = GetWorkbook(excelPath, out ws);

//Populate data in to new worksheet
//Code to populate values into new worksheet

ws.CalculateFormula(true, true, null);

//Read Values from new worksheet ws

//Remove the new worksheet - This line sporadically fails
wb.WorkSheets.RemoveAt(ws.Name);
}

I have tried to use wb.WorkSheets.RemoveAt(index) too. Since this is a multi-threaded application, I believe it is deleting internally based on some index value and deletes different sheet resulting in sporadic errors.

Could you please shed some light into this issue?

Thanks

Hi,

Thanks for your posting and using Aspose.Cell for .NET.

Please download and try the latest version: Aspose.Cells
for .NET v7.3.5.1
and see if it makes any difference.

If problem still occurs, then please provide us your runnable console project so that we could look into your issue precisely and in detail.

We will investigate your issue and help or advise you asap.

Here is the console app for your review. Please change app.config paths.

I believe that the issue is related to AddCopy creates a copy at a index and RemoveAt is happening at the same time in a different thread and deletes one other index which results in the sporadic errors.

Thanks

Is it possible to return the sheet name from AddCopy method instead of index? Since index can change in a multithreaded application (when add and remove sheet happens), if the sheet name is unique (should be unique), both AddCopy and RemoveAt can work on names parallely, instead of index.

Thanks

krishnb:

Is it possible to return the sheet name from AddCopy method instead of index? Since index can change in a multithreaded application (when add and remove sheet happens), if the sheet name is unique (should be unique), both AddCopy and RemoveAt can work on names parallely, instead of index.

Thanks

Hi,

Thanks for your sample project and using Aspose.Cells for .NET.

We have already logged your issue in our database. Please spare us some time to look into your issue and requirements. We will implement them and update you asap.

We will also analyze your requirements of AddCopy to return sheet name instead of index and implement it if possible.

This issue has been logged as CELLSNET-41372.

Hi,

Thanks for using Aspose.Cells.

Please do not process a single workbook object in a multi-threaded application at the same time.

You can construct different workbook objects for the same file.

As an example, here I have created three workbook objects from a single source Ms-Excel file. You can use this same approach to get rid of your problem.

C#


//Create 3 workbook objects from a single source Ms-Excel file.

Workbook w1 = new Workbook(filePath);

Workbook w2 = new Workbook(filePath);

Workbook w3 = new Workbook(filePath);