I have developed an Excel add-in that populates a sheet with data.
The main loop to populate the data is:
Excel.Worksheet sheet = workbook.Sheets.Add(After: workbook.Sheets[workbook.Sheets.Count]);
int newRow = 2;
// Llena la hoja con el maestro
foreach (var producto in maestro)
{
sheet.Cells[newRow, 1].Formula = producto.SKU.ToFormula();
sheet.Cells[newRow, 2].Value = producto.Descripcion;
sheet.Cells[newRow, 3].Value = producto.Linea;
sheet.Cells[newRow, 4].Value = producto.Familia;
sheet.Cells[newRow, 5].Value = producto.UltimoCorrelativo;
sheet.Cells[newRow, 6].Value = producto.FechaCreacion;
sheet.Cells[newRow, 7].Value = producto.FechaModificacion;
newRow++;
}
All works perfectly, however, since this process is run in a STA thread in order to not freeze the UI, a user can do other actions while processing. One of those actions is to click on a cell. That action causes the application to crash inside the for loop, in any instruction.
Exception thown is HRESULT: 0x800AC472
How can avoid that?