CustomXMLParts.Add 由于 ContextSwitchDeadlock 而变慢

CustomXMLParts.Add slow due to ContextSwitchDeadlock

我得到一个

ContextSwitchDeadlock

在执行 Documents.Add() 后添加 CustomXMLPart 时。

同样的代码上周运行良好..

我知道 ContextSwitchDeadlock 是由长时间的 运行 操作引起的(这不是重复的问题)。 为什么 CustomXMLParts.Add() 命令会导致长时间的 运行 操作?

有人遇到过这个吗?以及如何解决问题的任何想法?

ContextSwitchDeadlock occurred Message: Managed Debugging Assistant 'ContextSwitchDeadlock' has detected a problem in 'C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE'. Additional information: The CLR has been unable to transition from COM context 0xfdb520 to COM context 0xfdb468 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

调试较长的 运行 进程时可能会出现上下文切换死锁。大多数情况下,如果流程预计会很长,您可以忽略它 运行.

previous Whosebug answer