toosensitive
toosensitive

Reputation: 2375

Multiple thread access to Excel

I have two methods, method1, method2, both have lock(App) where App is a static instance in the same class.

App is a static instance in a shared project Helper

void method1()
{
    lock(Helper.App)
    { 
       ExcelApp.FormulaCell.AddComments(errMsg);
    }
}

    void method2()
    {
        lock(Helper.App)
        {  
           //plot data (an 2-D array) to cells
           var dataRng = ExcelApp.CurrentSheet.Cells(Formulacell.Row + 1, Formulacell.Column + 1);
           dataRng = dataRng.get_Resize(n, m);
           dataRng.Value2 = data;
        }
    }

method1, method2 are called by many threads. I want at one time only method1 or method2 run The issue is they seem not.
in method1 & method2, I plot some data to Excel, I get Exception

Name:COMException Message:Exception from HRESULT: 0x800AC472

I googled and find the exception is caused by Excel Application is not ready. There is no UI gesture, so I conclude the exception is caused by the two methods try to write to Excel at the same time.

Upvotes: 1

Views: 771

Answers (1)

Ofir
Ofir

Reputation: 2194

Please check if youre calling an async methods to excell.
If so, this is the problem, even though you used locks, the actual calls are running in the same time.
Try to wait till the excell methods ends.
Ofir

Upvotes: 1

Related Questions