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";
if (!ExcelCache.TryGetValue(excelPath, out wb))
wb = new Workbook(excelPath);
int index = wb.Worksheets.AddCopy(InputOutputSheetName);
ws = wb.Worksheets[index];
public void Calculate(string excelPath)
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
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?