DarkDeny DarkDeny - 1 month ago 21
C# Question

How can I avoid exceptions "app is busy" when working with MS Project via com interop?

We have an application which can export and import data to/from MS Project. Amount of data can be significant - 1000+ tasks.
But even with small data portions we noticed strange Project behavior - it can throw "app is busy" exception in your face when you do more than 1 operation in a row. E.g. create a new task, and read its finish date.
It can even throw this exception as a first operation after you just created Application object. Meaning:

var projectApp = new Application();

At the second line we can catch this exception just because Project app is still loading.

I tried to add Thread.Sleep, but this is not a good way, because some operations require more time than the others, and amount of time needed for operation is different on different computers (due to performance).

So the questuin is:
Is there a way to detect that Project app is busy without getting these unprectable exceptions all the time?


Applying STA to a thread task and using OleMessageFilter Register/Revoke found via google solved the problems! Thanks to Hans Passant for his comment which led me to this answer!