0

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
using System;
using System.Collections;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

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

    trayMenu.MenuItems.Add("Exit", ExitTrayIconClick);

    trayIcon.ContextMenu = trayMenu;
    trayIcon.Visible = true;

    System.Windows.Forms.Application.Run();
});

TrayThread.IsBackground = true;
TrayThread.Start();

/*
    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?

None
  • 609
  • 7
  • 22
  • The Exit() call is made on the wrong thread. Right now there is no way to make that call on the correct one, consider [this code](https://stackoverflow.com/a/21684059/17034). Note the usage of SychronizationContext and how Dispose() uses it to ensure that the thread terminates. Don't forget to call trayIcon.Dispose(). – Hans Passant Nov 18 '17 at 09:33
  • Why are you explicitly creating a thread? I hope this isn't in a console app –  Nov 18 '17 at 09:33
  • @HansPassant, so... I need to call Application.Exit() on the background thread? MickyD, this is a console app. – None Nov 18 '17 at 09:37
  • When is a console app not a console app? When it's displaying a GUI. Don't do that, make a proper WinForms/WPF app instead. If you need to emulate a terminal you can do so in a GUI and still have all the bells and whistles of a GUI app –  Nov 18 '17 at 15:00
  • @MickyD, It's actually a Unity game, but I tried the exact same code in my script in a console app and I saw the same result, so I thought relating the whole thing to a console app is easier to grasp. – None Nov 21 '17 at 19:15

0 Answers0