Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NotifyIcon在WPF上添加第一个ToolStripMenuItem时冻结应用程序一段时间_C#_Wpf_Notifyicon - Fatal编程技术网

C# NotifyIcon在WPF上添加第一个ToolStripMenuItem时冻结应用程序一段时间

C# NotifyIcon在WPF上添加第一个ToolStripMenuItem时冻结应用程序一段时间,c#,wpf,notifyicon,C#,Wpf,Notifyicon,我编写了以下代码来初始化WPF应用程序的NotifyIcon: var Notify = new System.Windows.Forms.NotifyIcon(); var sri = Application.GetResourceStream(new Uri("/Content/Images/icon.ico", UriKind.Relative)); var bitmap = new System.Drawing.Bitmap(sri.Stream);

我编写了以下代码来初始化WPF应用程序的NotifyIcon:

var Notify = new System.Windows.Forms.NotifyIcon();
        var sri = Application.GetResourceStream(new Uri("/Content/Images/icon.ico", UriKind.Relative));
        var bitmap = new System.Drawing.Bitmap(sri.Stream);
        var handle = bitmap.GetHicon();
        Notify.Icon = System.Drawing.Icon.FromHandle(handle);
        System.Windows.Forms.ContextMenuStrip Menu = new System.Windows.Forms.ContextMenuStrip();
        Notify.ContextMenuStrip = Menu;
        System.Windows.Forms.ToolStripMenuItem ExitMenuItem = new System.Windows.Forms.ToolStripMenuItem("Exit");
        ExitMenuItem.Click += (s, e) =>
        {
            Application.Current.Shutdown();
        };
        Menu.Items.Add(ExitMenuItem);
应用程序冻结在以下行(有关详细信息,请参阅下文):

它在许多机器上运行良好,但有时在少数其他机器上会将应用程序冻结2分钟。我遇到此问题的一台机器如下所示:

联想G50 8GB Ram 64位Windows 10家用intel Core i7-5500U CPU 2.40 千兆赫


在多次尝试测试之后,我发现只有在创建第一个ToolStripMenuItem并将其添加到NotifyIcon的ToolStripMenu(在本例中为ExitMenuItem)时,才会出现此问题。但我不明白为什么会这样。我已经标记了应用程序开始冻结的行。非常感谢您的帮助。

下一票背后的原因?共有3行,哪一行是准确的?我已更新了我的问题,以显示导致应用程序冻结的准确行。如果您移动该行
Notify.ContextMenuStrip=Menu到最后?我的意思是,只有在菜单完全构建之后,才能将其分配给NotifyIcon。而且它在特定的机器上是稳定复制的?谢谢你的评论。我已经试过了,问题仍然存在。
        System.Windows.Forms.ToolStripMenuItem ExitMenuItem = new System.Windows.Forms.ToolStripMenuItem("Exit");