Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: System.Windows.Forms.Application.Exit() not terminating System.Windows.Forms.Application.Run()

None:

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?



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: System.Windows.Forms.Application.Exit() not terminating System.Windows.Forms.Application.Run()

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×