This is the continuation of:
Find Window By Caption what is the caption of the window?
to cut it short i have a logger that stays in tray.
Then I have a program A that launches program B. I keep track of the situation throught the icon:
- red no program B launched
- blue program B launched but idle
- green program B running
So the normal cycle colours would be: red - blue - green - red
If I launch A--->B with no logger nothing bad arises
The logger stays resident and with a timer that scans processes when found process B it gets a connection to it through events.
private void TimerCheck(object sender, EventArgs e)
{
Process[] procList = Process.GetProcessesByName("PCDLRN");
if(procList.GetLength(0) != 0)
{
Stream iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/Resources/Images/Blueball.ico")).Stream;
notifyIcon.Icon = new System.Drawing.Icon(iconStream);
notifyIcon.Visible = true;
Thread.Sleep(1000);
pcdApplication = new PCDLRN.Application();
pcdApplication.WaitUntilReady(500);
pcdEvents = pcdApplication.ApplicationEvents;
pcdEvents.OnStartExecution += PcdEvents_OnStartExecution;
pcdEvents.OnEndExecution += PcdEvents_OnEndExecution;
timerCheckPCDmis.Stop();
}
}
When program B starts executing the icon gets green
private void PcdEvents_OnStartExecution(PCDLRN.PartProgram PartProg)
{
Stream iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/Resources/Images/GreenBall.ico")).Stream;
notifyIcon.Icon = new System.Drawing.Icon(iconStream);
notifyIcon.Visible = true;
}
instead when it has finished it gets red again
private void PcdEvents_OnEndExecution(PCDLRN.PartProgram PartProg, int TerminationType)
{
Stream iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/Resources/Images/RedBall.ico")).Stream;
notifyIcon.Icon = new System.Drawing.Icon(iconStream);
notifyIcon.Visible = true;
pcdEvents.OnStartExecution -= PcdEvents_OnStartExecution;
pcdEvents.OnEndExecution -= PcdEvents_OnEndExecution;
timerCheckPCDmis.Start();
}
But instead it get red and then blue. So the process is still alive and it is kept alive by my examination. Is it a zombie process?
Please notice that if I do ctrl+alt+canc and look for processes the name of the B program is not found there. So windows does not keep track of zombies? That said I could kill it anytime with
Process[] procList = Process.GetProcessesByName("PCDLRN");
if (procList.GetLength(0) != 0)
procList[0].Kill();
but this could arise other problems so it's way better to understand why it's alive.
The only think that I can think it's keeping it alive is the event connection but i do
pcdEvents.OnStartExecution -= PcdEvents_OnStartExecution;
pcdEvents.OnEndExecution -= PcdEvents_OnEndExecution;
so what else is keeping it alive?
---EDIT for emoacht---
I'll try to make it simplier sorry. I have a logger that stays resident in the tray.
Than a user can launch a program. This is not a program of mine it's made by another company. I only have a library that exposes some members and events. The ones I am interested in are StartExecution and EndExecution.
So my program stays silent and with a timer checks when the process of the external program arises.
When this happen I make a connection through events onStartExecution and onEndExecution.
When onEndExecution I detach from the events with pcdEvents.OnStartExecution -= PcdEvents_OnStartExecution; and pcdEvents.OnEndExecution -= PcdEvents_OnEndExecution;
And from that the timer restarts awaiting for the external program process.
The trick is that from that point the timer IMMEDIATELY tells that the external process is alive. But if I go to the TASK MANAGER i don't see it. Instead I can kill by looping on processes and killing it.
Since I have been downvoted I think I have to be even more precise with my questions. So:
Is this "waiting for process - connecting - waiting for end - detaching " strategy correct
A process no visible in task manager but present while enumerating processes is a zombie
Last how to avoid zombies....
---ADD--- I have an additional information. When I stop and restart the logger program the aforementioned zombie dies! So it really is my logger that keeps the zombie alive