Hi Danny,
I don't know of a specific reason why your call sequence would cause Excel to hang.
When you say that Excel 'hangs', do you mean that Excel stops responding completely, even if you wait a long time?
Is the CPU of the Excel process running high (e.g. pegged at 25%) when it is hanging, or is there no activity from the process in the Task Manager?
I can make some general comments and suggest a path to understanding things a bit better.
Firstly, writing to the sheet from inside a UDF is not allowed by Excel. By using the QueueAsMacro approach you get around this limitation, but you're still introducing a sequence of events that Excel might not be expecting.
If at all possible, I'd strongly suggest you move the whole formula write routine into a macro that is triggered by a user action like a context menu or ribbon callback.
In your case, perhaps Excel triggers a recalculation of the original formula for some reason while you are busy doing the writes - I'm not sure.
At least as a test, I suggest you try to start whole write routine from a macro, without the UDF as the start and without the QueueAsMacro(...). That would at least eliminate one potential source of trouble.
Secondly, you are interacting with an RTD server inside your MySlaveUDF. I'd suggest you test everything without the RTD part - maybe just return a dummy value from MySlaveUDF. It would help us to know whether the RTD part is an essential ingredient in the problem (I would expect it to be).
How is the RTD server implemented?
Is it your own RTD server or from a third-party?
If it's your own, is it using the thread-safe ExcelRtdServer base class in Excel-DNA?
The deadlock seems to be in a COM call from Excel to the RTD server. That's a point where the threading is quite important - everything on the Excel side is happening on the main thread, which might at this point be running the message loop waiting for a COM call to complete. If the RTD COM server is trying to notify Excel of an update at the same time, maybe there is a problem.
However, you message suggests that the failure is always exactly at the same place. If so, it might help as a test to add some short delays in your MySlaveUDF function, to see whether the change in timing would change the deadlock behaviour.
As a next step, you might replace the RTD server with a simple one - maybe the example in Distribution\Samples\RTD\ExcelRtdServer.dna in the Excel-DNA distribution. That might distinguish between an issue with the RTD server and something internal to Excel.
Finally, you might take a different approach when writing the formulae. For example, you could set calculation to manual during the write. That should prevent the recalculations triggering the RTD calls from being interleaved with your code, and should be faster.
A small but complete example that exhibits the problem could also be a great help to debugging the problem.
Regards,
Govert