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.
method1()andmethod2()being run at the same time?lock(App)produce a compile error becauseAppis a type, not not aTypeobject. For future reference, you should post the compile error that you are getting when you pose questions like this.