2008-09-19 68 views
10

我正在研究WPF应用程序,该应用程序有时会出现奇怪的问题,并且似乎在UI中挂起了。它不一致,它发生在不同的页面中,但它经常发生,这是一个很大的问题。我应该提到,它不是一个真正的悬挂,如下所述。如何解决WPF UI问题?

我的第一个想法是,一些按钮的动画是大多数页面上使用的问题,但是在删除它们之后仍会出现挂起,尽管看起来不太常见。发生挂起时,我试图打入调试器;但是从来没有任何代码可以查看。我的代码没有运行。我也注意到“挂起”并不完整。我有代码,可以让我拖动表单(它没有边框或标题),继续工作。我也有我的赢了关闭按钮,当我点击它时发挥作用。点击按钮看起来实际上可以在我的代码运行时正常工作,但UI不会更新以显示新页面。

我正在寻找任何建议,工具或技术来追踪这个奇怪的问题,所以如果你有任何想法,我将不胜感激。

编辑︰它再次发生,所以这次当我试图打入调试器,我选择“显示反汇编”。它带我到MS.Win32.UnsafeNativeMethods.GetMessageW。堆栈跟踪如下:

[Managed to Native Transition] 

WindowsBase.dll中MS.Win32.UnsafeNativeMethods.GetMessageW(REF System.Windows.Interop.MSG味精,System.Runtime.InteropServices.HandleRef的HWND,INT uMsgFilterMin,INT uMsgFilterMax )+ 0x15 bytes
WindowsBase.dll!System.Windows.Threading.Dispatcher.GetMessage(ref System.Windows.Interop.MSG msg,System.IntPtr hwnd,int minMessage,int maxMessage)+ 0x48 bytes WindowsBase.dll!System .Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame = {System.Windows.Threading.DispatcherFrame})+ 0x8b bytes WindowsBase.dll!System.Windows.Threading.Dispatcher。 PushFrame(System.Windows.Threading.DispatcherFrame frame)+ 0x49 bytes
WindowsBase.dll!System.Windows.Threading.Dispatcher.Run()+ 0x4c bytes
PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore )+ 0x1e bytes
PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window)+ 0x6f bytes PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window window)+ 0x26字节 PresentationFramework.dll!System.Windows.Application.Run()+ 0x19字节 WinterGreen.exe!WinterGreen.App.Main()+ 0x5e字节C# [原产于托管过渡]
[托管到NATIV e转换]
mscorlib.dll!System.AppDomain.nExecuteAssembly(System.Reflection.Assembly assembly,string [] args)+ 0x19 bytes mscorlib.dll!System.Runtime.Hosting.ManifestRunner.Run(bool checkAptModel)+ 0x6e字节 mscorlib.dll中!System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()+的0x84字节 mscorlib.dll中!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext,串[] activationCustomData)+ 0x65字节 mscorlib程序.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationContext activationContext)+ 0xa bytes mscorlib.dll!System.Activator.CreateInstance(System.ActivationContext activationContext)+ 0x3e bytes
Microsoft.VisualStudio。 HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()+ 0x23 bytes
mscorlib.dll!System.Threading。ThreadHelper.ThreadStart_Context(object state)+ 0x66 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state)+ 0x6f bytes
mscorlib.dll! System.Threading.ThreadHelper.ThreadStart()+ 0x44字节

回答

5

尝试删除窗口的无边框行为,看看是否有帮助。另外,您是BeginInvoke()'ing还是Invoke()'长时间运行的操作?

另一个要看的事情是:当你闯入你的代码时,试着看看你的主线程以外的线程。其中之一可能会阻止UI线程。

+0

有些时候某些异步进程被执行;然而,这种行为可能随时发生,包括在启动后几乎没有任何运行时发生。 我会尝试删除无边界行为。 – palehorse 2008-09-19 18:23:40

+0

仅需注意其他人将来阅读此内容:从不在调度程序中调用()或BeginInvoke()长时间运行的操作。你的用户界面似乎挂起...使用线程或BackgroundWorker或类似的东西。 – 2008-09-19 18:25:34

+0

Bob是对的,如果你一次在UI线程上做的太多,UI将变得无法响应。你想在UI线程上做尽可能少的事情;任何大的操作都应该在后台线程上完成。 – 2008-09-19 18:28:39

4

一个很棒的工具是Snoop。真的很适合查看WPF对象在给定时间在可视化树上显示的内容。我不确定它会提供多大的帮助,但是它可能会干扰UI线程并带来很多额外的事情。 Snoop可以帮助您追踪屏幕上的内容,为您提供想法。

+0

由于代码挂起,探听者可能无法帮助,但提问者无法进入挂起状态。 – 2008-09-19 18:23:17

6

由于性能问题,您的WPF应用程序可能会挂起。尝试使用Perforator来查看是否有任何软件呈现的部分,或者您的应用是否使用了太多的视频RAM。

4

我已经删除了Bob King建议的无边界行为。迄今为止,这似乎已经摆脱了这个问题。

现在的问题是,为什么以及如何解决这个问题?该产品设计为无边框,有一些圆角和透明部分。

1

好哇,看来这个问题与无边框窗口无关(至少在我的情况下)。

当您将AllowsTransparency设置为true时会有很大的性能下降。看起来如此之大,整个事情可能会挂起UI线程。非常奇怪的行为。可能与this ticket有关