5

我发现了一个非常奇怪的问题,如果我使用 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 来更改渲染层,但可以通过强制您的显卡在显示属性中使用软件渲染来完成。

4

1 回答 1

3

也许这有帮助:

如果在基于 Windows 2000 或基于 Windows XP 的计算机上运行的另一个程序不能正确处理 Windows 消息循环,则使用 DDE 的程序将停止响应。

你可以在这里找到它。

这只发生在 32 位 Windows 中,并且只有 DDE 客户端受到影响。为了建立连接,应用程序向所有顶层窗口广播一条消息。如果窗口不在同一个线程上,则消息实际上被发送到收件人队列并且调用者被阻塞。如果收件人没有消息队列,则调用线程被永久阻塞。微软承认这是一个错误。

更多详细信息在知识库文章Q136218 BUG:DdeConnect Never Returns 中。

于 2011-06-09T03:51:17.780 回答