5,422,921 members and growing! (17,956 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

How to kill processes running on a computer in your home

By Xiangyang Liu 刘向阳

A small ASP.NET program to help control your child's game playing
C# (C# 1.0, C#), .NET (.NET, .NET 1.1), Architect, DBA, Dev, Design, SysAdmin

Posted: 3 Jul 2008
Updated: 19 Jul 2008
Views: 7,977
Bookmarked: 31 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 4.45 Rating: 4.27 out of 5
1 vote, 9.1%
1
1 vote, 9.1%
2
0 votes, 0.0%
3
4 votes, 36.4%
4
5 votes, 45.5%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Like many other teenagers, my son spends too much time playing games and chatting with his friends online. Failing to get him to listen to me, I started to write a program that controls his computer. I hope you will never need to do the same. But for those parents who like the idea, here is what I came up with.

  1. An ASP.NET program that allows you to kill selected programs on another computer if these programs have been running for too long. You get to define how long is "too long".
  2. A little client program to make sure that your ASP.NET program is running even after rebooting the machine.

The programs you want to control are defined in the web.config file, like the following:

<appSettings>
<add key="GameList" value="firefox.exe,60,60;iexplore.exe,60,60" />
</appSettings>

As you can see, the value of GameList is a list of semi-colon delimited items. Each item has three parts separated by comma, the first part is the name of executable file, the second the number of minutes you allow the program to run. In this case, the application will kill all Fire Fox and Internet Explorer processes 60 minutes after they were detected. The third part is the number of minutes user has to wait before he/she can run the killed programs again. If a killed program is started before the waiting period ends, it will be killed again. The second part is called GameTime, the third part is called GameDelay.

You don't have to specify time values for each program. If you only have the executable names for some programs listed in GameList, then by default each of such programs is allowed to run 30 minutes and after that the user has to wait for 120 minutes. You can change the default values by defining GameTime and GameDelay in the appSettings section of the web.config file.

By default the application checks for programs defined in GameList every 15 seconds. The value of CheckInterval can be used to specify a new value (number of seconds).

To prevent users from renaming executable files to get around the restriction, you can specify folder path instead of executable name in GameList. For example, if the string c:\mygame\ is listed in GameList, then all programs in folder c:\mygame will be under control.

The Game Killer

This is an ASP.NET 1.1 web application. You have to install it on the computer you want to control. Here are the steps to install:

  1. Unzip the files into a folder on the target machine.
  2. Create a new virtual directory pointing to the GameKiller subfolder.
  3. If you have more than one versions of .NET frameworks on this computer, make sure the virtual directory is configured to run with .NET 1.1.
  4. Change machine.config file of .NET 1.1 framework so that it has the "SYSTEM" privilege. This can be done by finding the ProcessModel element in machine.config, replace value of userName attribute with "SYSTEM".
  5. Set folder permission properly so that non-admin users won't be able to delete or modify files belonging to this application.

Yes, #4 above is necessary because otherwise the application won't be able to detect and kill other running processes.

Assumptions:

  • You are doing this on one of your home computers behind firewall.
  • Your computer has .NET 1.1 framework installed and IIS enabled.
  • Your child does not have the necessary knowledge and skill to play the same trick on you (yet).

What if the machine you are trying to control does not have IIS? You can enable IIS on Windows XP Media Center Edition or Windows Vista Home Premium Edition. If you have Windows XP Home Edition, there is a way to install IIS on it if you have the Disks for Windows 2000 (I don't remember the details, you can easily google it).

How do you make sure the above ASP.NET application is running all the time, even after reboot? This is what the included PingClient.exe is for. All you have to do is schedule the following command to run at the start of the computer:

PingClient.exe http://localhost/GameKiller/Ping.asmx

The URL above is pointing to a web service within the ASP.NET application.

How Does It Work?

When the application starts up, it will read the list of programs and the corresponding times values from the GameList setting in web.config file and store the information in an internal array list. Then it will start a background thread which calls Process.GetProcesses to retrieve all running processes periodically.

For each running process, the application will check if it is one of the programs defined in web.config file. If it is, the time of detection will be stored into an internal hash table. If the same program is detected again after the allowed time (since its first detection), it will be killed by the application. Even if user restarts the program, it will be killed again until the time period specified in the GameDelay setting has passed.

Suppose a program is allowed to run for one hour. The user may run it for 59 minutes, reboot the machine and run it again, hoping to get another full hour's time. However, this trick won't work because the above internal hash table (which contains information on when a controlled program was first detected) will be saved to hard disk when the machine is shutting down. The data will be read into the internal hash table when the application starts up again. The application also has code to guard against setting the clock backward or forward.

User Interface

The main work is done in the background thread of the web application. However, the application does have a user interface. Here is what the GameKiller.aspx page looks like:

Display: This page displays all processes running on the computer. It lists process id, time (the number of minutes the process has been running), and file path for all processes. If you click the Display button, the list will be refreshed.

Filter: If you enter text into the Input text box and click this button, then only the processes whose paths contain the text you entered will be displayed. For example, if you want to see only programs located on the two folders D:\MyGame1 and D:\MyGame2, you can enter D:\MyGame1\;D:\MyGame2\ and click the filter button. Note that the two filter strings are separated by semi-colon.

Reverse Filter: This button does the opposite thing as the Filter button. If you don't want to see programs in the c:\windows and c:\backup, you can enter c:\windows\;c:\backup\ into the Input text box and click this button.

Add: You can dynamically add a new program into the list of programs you want to control from this page. For example, if you type NewGame.exe,5,30 into the Input text box, and click the Add button. Then NewGame.exe will be controlled and it will be allowed to run at most 5 minutes, and after that user has to wait for 30 minutes before running it again. If the program you want to add is already being controlled, then only the time values will be updated.

However, the changes made with the Add button wil not be written into the web.config file, therefore it will be lost after rebooting the machine.

Kill: The Kill button kills a process immediately. All you have to do is type the process id into the Input text box and click the button.

Restart: This button restarts the background thread within the web application.

From the above picture you can see that the inetinfo.exe process is listed with process id 3400. What will happen if you kill that process? Well, it means the web application won't be running any more and you are given your child the freedom to play any game he/she wants for however long he/she wants, until the machine is rebooted!

The real version of the program I use at home has some other features and the GUI won't work without proper authentication. For simplicity I did not include everything in the published version.

History

7/19/2008: Updated code and article text. Fixed bugs in previous version. Added the new feature of specifying GameTime and GameDelay for each individual program defined in GameList.

7/9/2008: Added code to detect whether clock is set backward or forward. If it is, then all controlled programs will be killed immediately.

7/7/2008:

  1. Added the "How does it work?" section.
  2. Updated source code to add the following feature: Now each program defined in web.config file can have its own delay time. If "MyGame.exe,5,10" is an item in the GameList setting, then MyGame.exe will be allowed to run for 5 minutes, after the time is up, user has to wait for another 10 minutes to run it (to avoid being killed). If only "MyGame.exe,5" is specified in web.config file, then the global GameDelay setting will be used instead.

7/5/2008:

  1. Added Filter and Reverse Filter buttons.
  2. Modified how the Add button works. You can add multiple items at once.
  3. Wrote data to hard disk when the application is shutdown, the data will be read into the application when it starts up (without this rebooting the machine will get around the control implemented in the application).

7/3/2008: Initial version posted.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Xiangyang Liu 刘向阳



Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
Subject  Author Date 
Generalgood articlemembervegeta4ss8:07 9 Jul '08  
GeneralRe: good article [modified]memberXiangyang Liu10:51 9 Jul '08  
GeneralWindows ServicememberMember 190497011:48 7 Jul '08  
GeneralRe: Windows ServicememberXiangyang Liu12:07 7 Jul '08  
GeneralWhat would happen...memberWist9:19 7 Jul '08  
GeneralRe: What would happen...memberXiangyang Liu11:40 7 Jul '08  
GeneralAnother way...memberbcraun17:14 3 Jul '08  
GeneralThe point is ... (Re: Another way...)memberXiangyang Liu17:55 3 Jul '08  
GeneralRe: The point is ... (Re: Another way...)membershadyscience@yahoo.com0:53 21 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Jul 2008
Editor: Sean Ewington
Copyright 2008 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project