Author Topic: wxWidgets - Killing a Process  (Read 3663 times)

Offline pankajbhalla

  • Single posting newcomer
  • *
  • Posts: 3
wxWidgets - Killing a Process
« on: December 01, 2005, 01:37:19 pm »
Hi All.

I have a simple problem but it seems to me that the solution is not so simple.

Lets says I have a.exe and b.exe running.
If I endtask a.exe from Task Manager, I would like to have b.exe to be killed automatically and Vice versa.

Thanks and Best Regards.
PanB

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: wxWidgets - Killing a Process
« Reply #1 on: December 01, 2005, 02:10:29 pm »
Not easy, since Task Manager does not gently quit your application, but shoots it right between the eyes.

One solution would be to repeatedly get the list of running processes and terminate if the other app cannot be found (very inefficient).
Another possible solution (more a dirty hack) may be to have a static global instance of an object which terminates the other application in its destructor.
I am not sure if destructors are actually called when an application is killed, but it is worth a try.
The destructor technique works reliably when your application crashes - the configuration file in Code::Blocks is saved this way, so you never lose settings when the app terminates unexpectedly.
I can't make any claims about whether this works when the app is force-killed. It is quite possible that the OS will just take away your CPU slices and do a global free of everything it can find belonging to your app...
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline pankajbhalla

  • Single posting newcomer
  • *
  • Posts: 3
Re: wxWidgets - Killing a Process
« Reply #2 on: December 02, 2005, 05:54:08 am »
Thomas.

Thanks for your suggestion and time.
Actually, you are right. Thats not an easy problem.

You Said: One solution would be to repeatedly get the list of running processes.
----> As of now, I think thats the only solution we have. Please suggest what are the risk factors associated with this solution. I mean, delaying issues.

You said: I am not sure if destructors are actually called.
----> Actually, destructor does not get a call. All resources from the memory were forcibly removed, and OS just take away your CPU slices.

If you have any other solution, or any wage idea that came in your mind, please discuss.

Thanks and Best Regards.
PanB

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: wxWidgets - Killing a Process
« Reply #3 on: December 02, 2005, 12:11:27 pm »
Please suggest what are the risk factors associated with this solution. I mean, delaying issues.
No risk. You just burn CPU cycles.
Consider putting it into OnIdle, then at least you don't burn cpu time when you would need it in another place, then it should be ok.
But expect notebook users to complain because their fan will spin up... :)

Yet another solution might be to open a TCP connection or a named pipe or something. When one end is killed, the other end should be notified "connection closed".

Actually, you could think about using a named mutex too. If you hold a mutex and your process is killed, it should technically be unlocked by the OS (again, I don't know, but I think so). If that works, then it would be the easiest and most efficient way.
Both processes could create a named mutex and acquire the lock. Each process then spawns off a thread which acquires the other processes named mutex - it will block. When the OS terminates the other process, the Mutex is unlocked and the blocking thread wakes up, telling the other application to terminate.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."