0

I've written the following code to initialize NotifyIcon of my WPF application:

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);

Application freezes on the following line (see below for details):

        System.Windows.Forms.ToolStripMenuItem ExitMenuItem = new System.Windows.Forms.ToolStripMenuItem("Exit");

It works well on many machines, but sometimes freezes the application for a duration of 2 minutes on few other machines. One machine in particular that I am having this issue with is as follows:

Lenovo G50 8GB Ram 64bit Windows 10 Home intel Core i7-5500U CPU 2.40 GHZ

After several attempts of testing, i figured that this problem occurs only when the first ToolStripMenuItem is created and added to ToolStripMenu of NotifyIcon (In this case the ExitMenuItem). But I am unable to understand why this is the case. I have marked the lines where the application starts to freeze. Your help will be much appreciated.

Sannan Khan
  • 39
  • 1
  • 9
  • Reason behind downvote? – Sannan Khan Apr 11 '17 at 10:08
  • there are 3 lines, which line to be exact? – kennyzx Apr 11 '17 at 10:09
  • I have updated my question to show the exact line that causes the application to freeze. – Sannan Khan Apr 11 '17 at 10:15
  • What if you move the line `Notify.ContextMenuStrip = Menu;` to the last? I mean, only after the menu is fully constructed, then you assign it to the NotifyIcon. And it is stably reproducible on that particular machine? – kennyzx Apr 11 '17 at 10:19
  • Thank you for your comment. I've already tried that, the problem still occurs. – Sannan Khan Apr 11 '17 at 10:21
  • @kennyzx By any chance could it be an issue with the other machines, as it is working well in most machines i've tested in. If so, there must be a logical reason behind it that I could try and fix. – Sannan Khan Apr 11 '17 at 12:06

0 Answers0