我发现了一个非常奇怪的问题,如果我使用 ThreadPool 获得 .Net 应用程序的渲染层,它将挂起来自 Excel 的非常简单的 DDE 调用。在从 Excel 调用 DDE 的同时运行复杂的 WPF 应用程序时会出现此问题。我已经设法在下面的几行代码中重现了这个问题。
C# .Net 应用程序
//Need to reference PresentationCore.dll
class Program
{
private static int _renderTier;
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(x =>
{
_renderTier = RenderCapability.Tier;
Console.WriteLine(_renderTier);
});
Console.ReadLine();
}
}
Excel DDE 宏。
Sub Using_DDE1()
' Dimension the variables.
Dim Chan As Integer
Dim RequestItems As Variant
' Start a channel to Word using the System topic.
Chan = DDEInitiate("WinWord", "System")
' Requesting information from Word using the Formats item
' this will return a one dimensional array.
RequestItems = DDERequest(Chan, "Formats")
' Uses a FOR loop to cycle through the array and display in a message box.
For i = LBound(RequestItems) To 3
MsgBox RequestItems(i)
Next i
' Terminate the DDE channel.
DDETerminate Chan
End Sub
运行宏会在单独运行时弹出 3 个消息框。如果我在 c# 应用程序运行时尝试运行宏,它将挂起对 DDEInitiate 的调用。一旦 c# 应用程序关闭,excel 就会恢复活力。从主线程获取渲染层不会导致问题。我还注意到,如果调试器暂停,即使没有调用获取渲染层,宏也会挂起。
使用带有 Excel 2003、.Net3.5 和 .Net4 的 Windows Xp 以及带有 Excel 2010、.Net3.5 和 .Net4 的 Windows 7 复制了问题。
知道为什么会这样吗?这是 PresentationCore.dll 的错误吗?
谢谢你的帮助
[更新]
更改机器的渲染层似乎释放了这个“锁”(之后我不得不移动窗户)。我通过启动 NetMeeting 来更改渲染层,但可以通过强制您的显卡在显示属性中使用软件渲染来完成。