我有正是如此限定的附加的行为,..

    public static class FileBrowserBehaviour
{


    public static bool GetBrowsesOnClick(DependencyObject obj)
    {
        return (bool)obj.GetValue(BrowsesOnClickProperty);
    }

    public static void SetBrowsesOnClick(DependencyObject obj, bool value)
    {
        obj.SetValue(BrowsesOnClickProperty, value);
    }

    // Using a DependencyProperty as the backing store for BrowsesOnClick.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BrowsesOnClickProperty =
        DependencyProperty.RegisterAttached("BrowsesOnClick", typeof(bool), typeof(FileBrowserBehaviour), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(BrowsesOnClickChanged)));


    public static void BrowsesOnClickChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        FrameworkElement fe = obj as FrameworkElement;

        if ((bool)args.NewValue)
        {
            fe.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(OpenFileBrowser);
        }
        else
        {
            fe.PreviewMouseLeftButtonDown -= new MouseButtonEventHandler(OpenFileBrowser);
        }
    }

    static void OpenFileBrowser(object sender, MouseButtonEventArgs e)
    {
        var tb = sender as TextBox;
        if (tb.Text.Length < 1 || tb.Text=="Click to browse..")
        {
            OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Executables | *.exe";
                if (ofd.ShowDialog() == true)
                {
                    Debug.WriteLine("Setting textbox text-" + ofd.FileName);
                    tb.Text = ofd.FileName;
                    Debug.WriteLine("Set textbox text");
                }
        }
    }
}

这是一个不错的简单的附加行为,当你点击一个文本框,并把文件名中当你完成的选框弹开一个OpenFileDialog。

它的工作原理,也许40%的时间,但整个应用程序挂起的时间休息。在这一点上的调用堆栈看起来像这样 -

[Managed to Native Transition]  
  

WindowsBase.dll中!MS.Win32.UnsafeNativeMethods.GetMessageW(参照System.Windows.Interop.MSG味精,System.Runtime.InteropServices.HandleRef的hWnd,INT uMsgFilterMin,INT uMsgFilterMax)+ 0×15字节,点击       WindowsBase.dll中!System.Windows.Threading.Dispatcher.GetMessage(REF System.Windows.Interop.MSG味精,System.IntPtr HWND,INT minMessage,INT maxMessage)+ 0x48字节       WindowsBase.dll中!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame帧= {System.Windows.Threading.DispatcherFrame})+ 0x8b字节       WindowsBase.dll中!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame帧)×49 +点击字节       WindowsBase.dll中!System.Windows.Threading.Dispatcher.Run()+ 0x4c字节结果       PresentationFramework.dll!System.Windows.Application.RunDispatcher(对象忽略)+ 0X1E字节结果       PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window窗)+ 0x6f字节       PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window窗)+ 0×26字节       PresentationFramework.dll!System.Windows.Application.Run()+ 0x19字节       Debugatron.exe!Debugatron.App.Main()+ 0x5e字节C#       [原产于托管过渡],点击       [管理到纯过渡],点击       mscorlib.dll中!System.AppDomain.nExecuteAssembly(System.Reflection.Assembly组件,串[]参数)+ 0x19字节       mscorlib.dll中!System.Runtime.Hosting.ManifestRunner.Run(布尔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       mscorlib.dll中!System.Activator.CreateInstance(System.ActivationContext activationContext)+ 0x3E的字节结果       Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()+ 0×23字节结果       mscorlib.dll中!System.Threading.ThreadHelper.ThreadStart_Context(对象状态)+ 0x66字节结果       mscorlib.dll中!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext的ExecutionContext,System.Threading.ContextCallback回调,对象状态)+ 0x6f字节结果       mscorlib.dll中!System.Threading.ThreadHelper.ThreadStart()+ 0×44字节

现在,我已经看到了这种事情做一些异步的东西时,但之前有没有在那个点回事。唯一的线索是活着的UI线程!另外,我的总是获取当它挂的是最后的调试语句。

任何人都可以点我在正确的方向?这一次的驾驶我疯了!

有帮助吗?

解决方案

我有一个使用在WPF应用程序的WPF web浏览器控件一个非常简单的WPF应用程序。我也有完全一样的问题。在WPF WebBrowser控件冻结的时间大约75%,当我水平调整窗口的大小(用谷歌地球使用JavaScript推出)。我得到完全相同的堆栈转储如上。当我复制/运行不同的电脑上的可执行文件(所有正在运行的XP SP3)运行良好,从来没有挂。我也有一个更复杂的应用程序,是多线程也有类似的堆栈转储(也等待的线程库内部消息)这台电脑上,但不是另一个PC挂起。在WPF WebBrowser控件的应用程序有同样的问题,无论是针对的.Net 3.5或4.0。我用NETfx_Setupverifier验证.NET安装是正确的,但我仍然怀疑没有与.NET或导致该.NET一些COM程序问题 - COM互操作是不稳定的。这也是我的猜测,我的应用程序正在等待一些非托管事件/消息从未到达因为与互操作的问题。我也写了使用的WinForms / WinForm的WebBrowser控件一样简单的应用程序和应用从未挂在同一台PC上。

有没有人对如何追查病因有什么建议?我正在考虑完全卸载/重新安装.NET框架,即使他们核对无误。我甚至不知道到哪里寻找在COM端的异常。

其他提示

下面去一些几乎是随机的事实和问题,可以帮助你。

首先,我无法重现您的问题。无论我怎样试图。它总是奏效。

堆栈跟踪看起来也不错对我来说:它使处理消息循环。究竟混淆吗?托管到本机的过渡?

您不能在WPF应用程序有一个线程。你怎么在VS调试器的线程窗口看到了什么?

当我击打破了所有我看到在主线程堆栈跟踪调用ofd.ShowDialog(),并命名为.NET SystemEvents一个工作线程停留在WindowThreadProc(),等待一个同步对象。你看到了什么?

它看起来像你陷入的僵局莫名其妙。

尝试包裹OpenFileBrowser()内容到try... catch{}。是否有任何错误?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top