I'm using the NotifyIcon control in a console application, so I have to call the blocking
System.Windows.Forms.Application.Run() function in order to get the
NotifyIcon to work correctly(according to this). I am calling that function on a background thread to get along this. Theoretically, calling
System.Windows.Forms.Application.Exit() should terminate the
System.Windows.Forms.Application.Run() function and exit the background thread. I tried that, but the thread doesn't terminate! I even tried aborting the thread(Although I know this is a really bad idea), but that didn't work either. So... I can't kill the thread and I can't safely stop it. What The What??!
Some people would like to see some code, so here is what I generally do:
//Those are my usings
//This is what I do at the very beginning of the program
Thread TrayThread = new Thread(() =>
trayIcon.Text = "TestApp";
trayIcon.Icon = new Icon(SystemIcons.WinLogo, 40, 40);
trayIcon.Click += TrayIconClick;
System.Windows.Forms.ContextMenu trayMenu = new System.Windows.Forms.ContextMenu();
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
TrayThread.IsBackground = true;
A lot and a lot of code goes here...
//This is what I do when the application is exiting
System.Windows.Forms.Application.Exit(); // This should terminate the thread, but it doesn't...
TrayThread.Abort(); // This simply makes no difference. The thread didn't terminate with it. The thread didn't terminate without it. (I know it's horrible to use this function)
TrayThread.Join(); // That hangs forever...
How can I terminate that thread?
Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered