1

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.

8
  • 3
    Are you currently experiencing method1() and method2() being run at the same time? Commented Apr 23, 2013 at 16:30
  • 1
    I'm sorry, I couldn't figure out what the problem is. What have you coded so far? Are you getting any error? Commented Apr 23, 2013 at 16:30
  • What you have there should work. What problems are you getting? Commented Apr 23, 2013 at 16:30
  • Using lock(App) produce a compile error because App is a type, not not a Type object. For future reference, you should post the compile error that you are getting when you pose questions like this. Commented Apr 23, 2013 at 16:34
  • 2
    @JonSenchyna: You're assuming that App is a type but the OP specifies in the OP that `App is a static instance' in that class. Commented Apr 23, 2013 at 16:40

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.