I'm trying to do apply some styles to cells in my workbook. And I want to do it in background thread so my GUI can stay responsive. This job should take few seconds, and if I click on some random cell in my document I'll get an exception. Here is my code:
public void ApplyStyles()
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += DoWork;
bw.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
try
{
foreach (ICell xcell in cells)
{
Microsoft.Office.Interop.Excel.Range cell = cellUtility.GetCell(xcell);
if (styles.ContainsKey(styleIds[xcell.Style]))
{
Style s = styles[xcell.Style];
cell.Style = s;
}
}
}
catch (Exception ex)
{
if (Logger.IsErrorEnabled)
{
Logger.Error(ex.ToString());
}
messageBox.ShowErrorMessage(localizationMessages.ApplyingErrorText, localizationMessages.ApplyingErrorCaption);
}
}
When exception happens, this is the message I'm receiving;
System.Runtime.InteropServices.COMException (0x800AC472): Exception from HRESULT: 0x800AC472
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.Range.set_Style(Object value)
at ABZ.ReportFactory.OfficeAddin.Excel.BatchLinking.BackgroundStyleApplier.DoWork() in C:\ABZ\ABZ ReportFactory Office Addin\ABZ.ReportFactory.OfficeAddin.Excel\BatchLinking\BackgroundStyleApplier.cs:line 86
Is it possible to do this style applying operation in background thread? And how should I do it?
Second problem I have is that while this style applying is running in background my cursor is constantly changing state from busy to regular until this operation is over. I would like for cursor to be normal. That user is totally unaware of this background operation.
cheers, Vladimir