Den
Den

Reputation: 16826

Graceful Win32 windowless process termination

I am working on a console application, written in C#, where I trigger Application.Run() to manage the system tray. The application itself is window-less.

Screenshot of the application configuration in Visual Studio

Here's the challenge I am encountering - when the user closes the application, I need to do cleanup, and make sure that I remove the application icon from the system tray. Easy enough to do when I have a console window attached and allocated since I can use SetConsoleCtrlHandler.

However, the problems start when I do not have a console window allocated and attached (since the application runs like that most of the time). I want to gracefully terminate the process from another process, so that I can call SetConsoleCtrlHandler in my application and do the cleanup. But, all the guidance that I find on doing that for Windows is through sending WM_CLOSE messages, which would not apply since I do not have any windows available for the application.

So, considering that the application in question is launched with CreateProcess, I have the process ID, handle, and main thread ID.

Question: is there a way for me to gracefully tell the other application to terminate without calling TerminateProcess (which would not give any leeway to do any cleanup)?

I am thinking I might need to implement a named pipe so that I can communicate between the client application and the "server" in this case, but it feels like overkill for having a way to nicely ask the process to wrap things up.

Upvotes: 3

Views: 295

Answers (1)

Evgeny
Evgeny

Reputation: 1072

I think, you should implement own method of communication. IMHO, simplest way is using named event. Console application creates named event and sometimes checks it. External application sets event in case to exit first application. This method works for Windows only.

Upvotes: 3

Related Questions