.NET Compact Framework Kill All Programs

By Robbe D. Morris

Printer Friendly Version

Robbe Morris
Robbe & Melisa Morris
  Download Source Code
Here's a quick tip on how to kill all of the running programs on your mobile device.  The following code snippet uses an API call to start killing applications until it has freed up the designated amount of memory.  In this case, I've simply passed in the int.MaxValue so that it will kill everything.
I put this little sample together for two reasons.  The first being that I wanted to make sure applications that I was deploying updates to or removing weren't already running via this deployment tool I put together a little while ago:  .NET Compact Framework File Deployment Tool.  You could simply add some code in that sample to copy the killall.exe file to the \Program Files folder if it doesn't already exist and then execute it prior to running the deployment.
The second reason comes from the purchase of my new Sprint PCS 6600 PDA Phone and Microsoft's Voice Command software.  Voice Command is a fantastic piece of software that lets you do a variety of things including starting up any program.  Unfortunately, it doesn't come with a voice command for shutting down apps.  Now I can copy killall.exe to the \Program Files folder, create a shortcut to be placed in \Windows\Start Menu named Kill Everything, launch Voice Command and say:  start kill everything to automatically shut down all my apps.
Feel free to download the sample application above or peruse the sample code below.
 


 
C# Clean Kill Class
using System;
using System.Runtime.InteropServices;
using SmartDeviceApplication2;

namespace SmartDeviceApplication2
{

   public class CleanKill
   {

      public CleanKill() { }
	 
      [DllImport("aygshell.dll")]
      public static extern int SHCloseApps(int dwMemSought);
 
      public static bool Execute()
      {
                 
         try
         {

           if (SHCloseApps(int.MaxValue)!= 0)
           {
             return false;
           }
         }
         catch  { } 

         return true;
      }
 
  }
}
C# Form1
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using SmartDeviceApplication2;

  static void Main() 
  {
    Application.Run(new Form1());
  }

  
  private void Form1_Load(object sender, System.EventArgs e)
  {
     CleanKill.Execute();
     this.Close();
     Application.Exit(); 
  }

  

Robbe has been a Microsoft MVP in C# since 2004.  He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.