我發現了一個非常奇怪的問題,如果我使用ThreadPool獲取.Net應用程序的渲染層,它將從Excel中掛起一個非常簡單的DDE調用。在與Excel的DDE調用同時運行復雜的WPF應用程序時,發現問題。我已經設法在幾行代碼中重現了這個問題,這些代碼可以在下面找到。爲什麼我使用線程池獲得.Net應用程序的渲染層之後,我的Excel的DDE調用會掛起?
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就會恢復生機。從主線程獲取渲染層不會導致問題。我還注意到,如果調試器暫停,即使沒有獲取渲染層的調用,宏也會掛起。
使用Windows XP與Excel 2003,.Net3.5複製的問題& .Net4和Windows 7與Excel 2010,.Net3.5 & .Net4。
任何想法爲什麼會發生這種情況?這是PresentationCore.dll的錯誤嗎?
感謝您的幫助
[更新]
更改本機的渲染層似乎釋放這個「鎖定」(我不得不事後走動了一下窗戶)。我通過啓動NetMeeting來更改渲染層,但可以通過強制顯卡在顯示屬性中使用軟件渲染來完成。
嗨雅各,那個報價的來源是什麼? – 2011-06-09 09:23:02
嗨阿什利。更新了答案。 – 2011-06-09 12:41:47