C# – Hanging in Quitting the app

cnetwindows-mobile

I have an app runnig in my win-mobile.. it has assosiated with many DLL's.
If i install app in storage card, all DLL and .exe comes to storage card only..

My requirement is if i take out storage card app should quit..

I am using c# .net 3.5 CF for development. I will get notification tat storage card removed..but if i do KILL the process like Process.GetCurrentProcess().Kill().. application hungs up, ultimately i need to restart my mobile, I dont no how to handle this worst case… if i kill process also its not getting killed.. how to fix this problem…?

This case occurs because the app is installed in storage card, i know the cause also like all necessary dll will be moved out due to removel of card, but how to kill the process from task manager.? its headache for me.

Please, please help me!

Thank You!

Best Answer

This is a difficult one to answer, because as Chris pointed out to you in one of your other questions, you can't guarantee that any of the code will be in memory after the card is removed. For example, your code that handles the storage card removal notification may have been pitched.

One way would be to have a small application that installs to the device rather than the card, that monitors the storage card removal, and can then kill the main application if it detects the card has been removed.

If you want to get the list of running processes you can enumerate them using the toolhelp32.dll, you'll need to p/invoke some native calls to do it. See here for info on walking the processes. Basically create the toolhelp 32 snapshot, walk each process using process32first and process32next until you find your app and get the process id.

Given that you have a process Id you can then use these dll imports to open the process and terminate it:

[DllImport("coredll.dll", SetLastError = true)]
private static extern bool TerminateProcess(IntPtr hProcess, uint ExitCode);

[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr OpenProcess(int flags, bool fInherit, int PID);