none
How to make a global object Thread Safe in a session of Excel with multiple WorkBooks open RRS feed

  • Question

  • Hi,

    I have a issue here. I have developed an Application Level Addin using Excel 2007, VS 2010 and VSTO 4.0.

    It is working without any issues except for a very peculiar scenario.

    I have defined two events Application_WorkbookActivate(Workbook Wb) and Application_WorkbookDeactivate(Workbook Wb). As per the functionality of the Addin, when the user toggles between two workbooks, lets say from workbookA to WorkbookB, firstly Application_WorkbookDeactivate gets called and i do some actions in the event which involves reading a Global Object and DeSerializing the object and writing it to an Hidden sheet within the workbook. Then the event Application_WorkbookActivate gets triggered and this involves reading the text from the Hiddensheet and forming the Global Object again.

    Now if the user toggles at a typical pace of about 2-3 seconds between toggle, everything goes fine. But if he toggles very quickly, then the data in the hidden sheet is getting jumbled. That is the Data in the WorkbookA is getting written to the data in the WorkbookB and vice versa.

    This i believe is happening because i assume that Excel spams a thread for each workbook and in this scenario both the threads are running parallely when user is toggling between workbooks at a very fast pace. And this is causing both the threads to  read/write to the global object at the same time.

    I wanted to stop this from happening. Can someone please suggest me a solution. I tried using Lock concept but without any luck. Any sort of help is very much appreciated.

    Thanks in advance.

    Shailendra

    Friday, November 30, 2012 2:16 AM

Answers

  • i;m aftraid you are contradicting yourself - either i was right and they are executed in single thread (which means that at any single time only one event is executed) OR they are executed in parallel. it cannot be both ways. If former is true (and it is), you have to write your code in event handlers that will handle situations where single event is raised few times before the other one takes place.
    • Marked as answer by Shailendra_G Tuesday, December 4, 2012 4:32 PM
    Friday, November 30, 2012 5:54 PM

All replies

  • i do not think that excel raises events on different threads. you can check it yourself by looking at the Thread.CurrentThread.ManagedThreadId, it should be the same in both events. Let us know about your findings.
    Friday, November 30, 2012 7:52 AM
  • Hey Damian .. as you said, i looked at Thread.CurrentThread.ManagedThreadId and you were right. There is only thread. But the problem, when i added some logs to see if both the methods Application_WorkbookActivate and Application_WorkbookDeactivate are running synchronously and i see they are not.

    As per my logs, i can see that when i toggle quickly, these methods are not getting executed one after the other. They are getting executed in parallel and sometimes same method is getting executed twice even before the other is getting called. How do i make sure that these methods called synchronously.

    Friday, November 30, 2012 3:43 PM
  • i;m aftraid you are contradicting yourself - either i was right and they are executed in single thread (which means that at any single time only one event is executed) OR they are executed in parallel. it cannot be both ways. If former is true (and it is), you have to write your code in event handlers that will handle situations where single event is raised few times before the other one takes place.
    • Marked as answer by Shailendra_G Tuesday, December 4, 2012 4:32 PM
    Friday, November 30, 2012 5:54 PM
  • It may just be a single thread .... but since the toggle was too fast .... the sequence of steps was very inconsistent. So i ended up writing code in event handlers which was preventing other events to execute while this is still running .... but that was quite a pain debugging and resolving this issue.

    Thanks for your time on this.

    Tuesday, December 4, 2012 4:31 PM