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.