25 April, 2018

Forcing A Windows Application To Quit On Exit

English: Graphic which hints to Microsoft Windows
English: Graphic which hints to Microsoft Windows (Photo credit: Wikipedia)
25-April-2018

Forcing A Windows Application To Quit On Exit. That was my recent problem with a Windows Forms Application I was finalizing for deployment. It is not a big program. And it is not even one that is complicated. Straightforward and simple. That's what it is. But somehow, it carried a bug.

The .exe isn't terminated by Windows. It remains in the system. I can see it using Task Manager. And how did I find out about it? I am not able to execute a Ctrl-F5 properly. I always get an error.

That is when I started checking here and there, and I found out, the program's .exe remained in the system even after I close the program. Why is that? Beats me!

I opened one of my Windows Forms program, compared the code side-by-side, and no, there is nothing different. It is the same lines of code for the 2 programs, but the former closes and system disposes of everything. The latter, no. the Form closes, but the .exe remains in the system. So I can't start a new program since to the system, it is 'still running'!

Why it behaves that way, I do not know.

Now, I said this is a piece of cake. It is easy. I know how to terminate a program so that it is removed from the Taskbar. I was thinking that the same trick should work on this new program. I mean, why shouldn't it?

But I was wrong. It did not work! How can Visual Studio codes be so quirky?

Anyway, I did some direct codes, testing a few lines a couple of times. And guess what? Here's what worked:

Application.DoEvents();
if (Application.MessageLoop) {
    // WinForms app
    Application.DoEvents();
    Application.Exit();
}
else {
    // Console app
    Environment.Exit(0);
}

That 'if' section, it is meant to be for Windows Forms applications, and the 'else' clause, that is meant to be for non-Windows forms programs, like a Console application. That's the 'normal' way of things. Supposedly the 'right' logic.

But wait.

After trying it the normal way, it didn't work. So I tried it the extra-normal way: use Environment.Exit(0) in my Windows Forms application. And you'll guess it already. It worked!

I guess that is an experienced programmer's edge over the newbie. Not all the time, things work normally. And when it happens, the veteran knows how it is done rightfully, or maverick.

So I hope that you have learned a new thing today: Forcing A Windows Application To Quit On Exit.

Till then!


[Drafted on 26-March-2018]


11 April, 2018

Prevent Leaving An Icon In System Tray On Program Exit

The Windows 7 taskbar shows a preview of the w...
The Windows 7 taskbar shows a preview of the window. (Photo credit: Wikipedia)
11-April-2018


Prevent Leaving An Icon In System Tray On Program Exit. I had a problem with one of my applications about this. I was able to minimize it to System Tray. The App Icon was removed from the Taskbar, alright. But when the application was restored and closed, the icon remains in the System Tray. What's worse, I didn't notice it!

This was complicated by the fact that I did a routine cycle of closing and opening the program using scripts and some free apps downloadable from the internet, so that for a period of time, System Tray was filled up with icons of the same program, all N-1 of which are invalid handles!

I got a good scolding from my boss about this. Why? When I researched this problem, I found out that it is a prevalent issue. There are a number of forums that discusses the same exact problem, as obviously, I am not the only one having it. No, I am not the first one, and I will not be the last.

What did I find? That it was, and it remains, an issue by MS. Yep. The software giant company MS. They have it as a bug, and it still isn't fixed. The full details, I cannot tell here. But if you are interested, and if it happens to you in the future, you will know and understand what I mean.

Of course, there were suggestions presented in the forums to remedy the situation, and some of them I actually followed. Unfortunately, they didn't work perfectly for me. That is why so many icons still get left behind. And worse, this was implemented in one of our customers. Customer complain, that is what I got. That is how I got a good scolding. What's the point? I followed and applied in my code something that is 'wrong'. Yeah, I kinda missed that.

Anyway, how was that problem remedied?

This is the surprising this. It really is very simple, and it isn't something hidden.

In the FormClosed event, put (or add) these lines:

myNotifyIcon.Icon = null;
myNotifyIcon.Dispose();
Application.DoEvents();
if (Application.MessageLoop) {
    // WinForms app
    Application.DoEvents();
    Application.Exit();
}
else {
    // Console app
    Environment.Exit(0);
}

Surprising, isn't it? You gotta try it to know it! And with that, You should be all set. Prevent Leaving An Icon In System Tray On Program Exit.

Till then!

[Late posting; drafted on 18-October-2017]