.net - 为什么在我使用线程池获得 .Net 应用程序的渲染层后,来自 Excel 的 DDE 调用会挂起?

标签 .net wpf excel dde

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

最佳答案

也许这有帮助:

If another program that is running on the Windows 2000-based or on the Windows XP-based computer does not correctly process an Windows message loop, the program that uses DDE stops responding.

可以找到here .

This only happens in 32-bit Windows, and only DDE clients are affected. To make a connection, an application broadcasts a message to all top level windows. If the window is not on the same thread, the message is actually posted to the recipients queue and the caller is blocked. If the recipient has no message queue the calling thread is permanently blocked. Microsoft has acknowledged that this is a bug.

更多详细信息请参见知识库文章 Q136218 BUG:DdeConnect 永远不会返回。

关于.net - 为什么在我使用线程池获得 .Net 应用程序的渲染层后,来自 Excel 的 DDE 调用会挂起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6228857/

相关文章:

C# webbrowser 控件,更改右键单击选项

java - 创建用于在 Excel 中导入文本文件的脚本的最佳解决方案是什么?

php - 将php数组数据导出到excel

.net - "Cannot Resolve Symbol Select"尝试将 LINQ 与 DataRowCollection 一起使用

c# - 尝试使用 Linq to XML (C#) 解析 XML 树

c# - HTML Agility Pack - 只能从文件系统加载 xml 文档,不能从 Web

wpf - 在哪里可以找到 WPF 经典主题作为 XAML?

c# - 关闭窗口后出现 TaskCanceledException

.net - MVC解决方案结构

vba - 在工作表创建复制隐藏表 "TEMPLATE"